Note that there are some explanatory texts on larger screens.

plurals
  1. POcodeigniter, jQuery Mobile, jQuery UI - scripts not loading on first load, only refresh
    text
    copied!<p>I am having a confusing time with my codeigniter app.</p> <p>I have a view/form(view 1) which when submitted loads another view (view 2). view 1, on initial page load does not load the correct js and css. it instead loads the js and css from view 1. if I then hit refresh on view 2, the correct css and js is loaded.</p> <p>To prove my point I placed a test js script</p> <pre><code>&lt;script type='text/javascript'&gt; alert('test'); &lt;/script&gt; </code></pre> <p>as the first bit of code on both view 1 and view 2. on loading view 1 for the first time, the alert is shown, when I click submit on view 1 and view 2 is loaded the alert is not shown. if I refresh though the alert shows as desired.</p> <p>So js and css not loading correctly on initial load but on refresh works perfectly.</p> <p>Any ideas?</p> <p><strong>View 1</strong></p> <pre><code>&lt;script type='text/javascript'&gt; alert('test'); &lt;/script&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Capture New Order &lt;/title&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;div data-role="page" data-theme="a"&gt; &lt;div class="wrap-header"&gt; &lt;div data-role="header" data-mini="true" data-ajax="false"&gt; &lt;a data-icon="grid" data-mini="true" data-theme="a" onclick="window.location.href='/pointer'"&gt;Menu&lt;/a&gt; &lt;h3&gt;New Order Details&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; &lt;div data-role="content"&gt; &lt;form id="sales_order_details" action="/sales/new_blank_order_lines" method="post"&gt; &lt;label for="customer"&gt;Select Customer&lt;/label&gt; &lt;div id="autocomplete_dropdown"&gt; &lt;input type="text" id="customer" name="customer" /&gt; &lt;/div&gt; &lt;label for="period"&gt;Desired Order Period&lt;/label&gt; &lt;select id="period" name="period"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;?php for($i = 0; $i &lt;= 365; $i ++){ $startdate = strtotime("today + $i day"); $enddate = strtotime("today + " . ($i + 6) . " day"); if(date('D', $startdate) == 'Mon'){ echo '&lt;option value="' . date('Y-m-d', $startdate) . '"&gt;' .date('d M y', $startdate) . " to " . date('d M y', $enddate) . "&lt;/option&gt;"; } } ?&gt; &lt;/select&gt; &lt;label&gt;Choose an order type below and provide requested details&lt;/label&gt; &lt;div data-role="collapsible-set"&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Auto Spread Order &lt;/h3&gt; &lt;label for="asuom"&gt;Unit of Measure for Order&lt;/label&gt; &lt;select id="asuom" name="asuom"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;option value="Bundles"&gt;Bundles&lt;/option&gt; &lt;option value="Cubes"&gt;Cubes&lt;/option&gt; &lt;option value="Pieces"&gt;Pieces&lt;/option&gt; &lt;/select&gt; &lt;label&gt;Number of Cubes to Autospread&lt;/label&gt; &lt;input type="text" name="ascubes" id="ascubes"&gt; &lt;label&gt;Products over which to spread&lt;/label&gt; must work on this... &lt;input type="submit" name="previous_order" id="previous_order" value="Continue"&gt; &lt;/div&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Previous Order &lt;/h3&gt; &lt;label&gt;Select Order to use as template&lt;/label&gt; &lt;input type="submit" name="previous_order" id="previous_order" value="Continue"&gt; &lt;/div&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Blank Order &lt;/h3&gt; &lt;label for="buom"&gt;Unit of Measure for Order&lt;/label&gt; &lt;select id="buom" name="buom"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;option value="cubes"&gt;Bundles&lt;/option&gt; &lt;option value="cubes"&gt;Cubes&lt;/option&gt; &lt;option value="pcs"&gt;Pieces&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" name="blank_order" id="blank_order" value="Continue"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;!-- script for page start--&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function(){ $("#customer").autocomplete({ source: "get_customers", messages: { noResults: '', results: function() {} } }); }); &lt;/script&gt; </code></pre> <p><strong>View 2</strong></p> <pre><code>&lt;script type='text/javascript'&gt; alert('test'); &lt;/script&gt; &lt;?php $newperiod = date('Y-m-d',strtotime($period) + (24*3600*6)); ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Capture blank Order &lt;/title&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function callAutocomplete(element) { $(element).autocomplete( { source: "get_sku_codes", messages: { noResults: '', results: function() {} }, select: function( event, ui ) { var selectedObj = ui.item; $.post('sales/get_sku_prices', {data:selectedObj.value},function(result) { $(ui).parent('tr').find('input[id^="pricepercube"]').val(result); }); } }); } $(function() { var counter = 1; jQuery("table.authors-list").on('change','input[name^="qty"]',function(event) { event.preventDefault(); counter++; var newRow = jQuery('&lt;tr&gt;'+ ' &lt;td&gt;&lt;a class="deleteRow"&gt; &lt;img src="&lt;?php echo base_url() ?&gt;application/assets/images/no.jpg" /&gt;&lt;/a&gt;&lt;/td&gt;' + ' &lt;td&gt;&lt;input type="text" id="product' + counter + '" name="product' + counter + '" /&gt;&lt;/td&gt;' + ' &lt;td&gt;&lt;input type="text" id="qty' + counter + '" name="qty' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td&gt;&lt;input type="text" id="price' + counter + '" name="price' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td&gt;&lt;input type="text" id="discount' + counter + '" name="discount' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td valign=top&gt;&lt;input type="checkbox" id="treated' + counter + '" name="treated' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;/tr&gt;'); jQuery('table.authors-list').append(newRow); callAutocomplete("#product"+ counter); }); $("#product").autocomplete( { source: "get_sku_codes", messages: { noResults: '', results: function() {} }, select: function( event, ui ) { var selectedObj = ui.item; $.post('&lt;?=site_url("sales/get_sku_price")?&gt;', {data:value},function(result) { $("#price").val(result); }); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body &gt; &lt;div class="wrap-header"&gt; &lt;div data-role="header" data-mini="true" data-ajax="false"&gt; &lt;a data-icon="grid" data-mini="true" data-theme="a" onclick="window.location.href='/pointer'"&gt;Menu&lt;/a&gt; &lt;h3&gt;New Blank Order&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; &lt;div data-role="content"&gt; &lt;center&gt; &lt;table width=80%&gt;&lt;tr&gt;&lt;td&gt; &lt;input type="text" name="customer" id="customer" value="&lt;?php echo $customer; ?&gt;" disabled&gt; &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="period" id="period" value="&lt;?php echo $period." to ".$newperiod; ?&gt;" disabled&gt; &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="buom" id="buom" value="&lt;?php echo $buom; ?&gt;" disabled&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; &lt;/center&gt; &lt;/div&gt; &lt;table class="authors-list" border=0&gt; &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Product&lt;/td&gt;&lt;td&gt;Qty&lt;/td&gt;&lt;td&gt;Price/Cube&lt;/td&gt;&lt;td&gt;Discount&lt;/td&gt;&lt;td&gt;treated&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;a class="deleteRow"&gt; &lt;img src="&lt;?php echo base_url() ?&gt;application/assets/images/no.jpg" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="product" name="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="qty" name="qty" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="price" name="price" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="discount" name="discount" /&gt;&lt;/td&gt; &lt;td valign="top" &gt;&lt;input type="checkbox" id="treated" name="treated" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Initial Page Load</strong> - no alert or js/css working - only css and js from view 1 <img src="https://i.stack.imgur.com/N8hGB.png" alt="enter image description here"></p> <p><strong>Refresh Load</strong> - no css or js from view 1, only view 2 css and js loaded. note I have removed all styling from view 2 just to test and get working. So below image is what I desire. alert is shown here. this is load after clsing alert. <img src="https://i.stack.imgur.com/A4gLa.png" alt="enter image description here"></p> <p>I have tried this in IE, Firefox and Chrome all with same result.</p> <p>Any advice is appreciated. thanks</p> <p><strong>UPDATE</strong></p> <p>View 1:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt; Capture New Order &lt;/title&gt; &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /&gt; &lt;!-- script for page start--&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).on('pageshow', '#pagebody', function(){ $(function(){ $("#customer").autocomplete({ source: "get_customers", messages: { noResults: '', results: function() {} } }); }); }); &lt;/script&gt; &lt;!-- script for page end--&gt; &lt;/head&gt; &lt;body&gt; &lt;div data-role="page" data-theme="a"&gt; &lt;div class="wrap-header"&gt; &lt;div data-role="header" data-mini="true" data-ajax="false"&gt; &lt;a data-icon="grid" data-mini="true" data-theme="a" onclick="window.location.href='/pointer'"&gt;Menu&lt;/a&gt; &lt;h3&gt;New Order Details&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; &lt;div data-role="content"&gt; &lt;form id="sales_order_details" action="/sales/new_blank_order_lines" method="post"&gt; &lt;label for="customer"&gt;Select Customer&lt;/label&gt; &lt;div id="autocomplete_dropdown"&gt; &lt;input type="text" id="customer" name="customer" /&gt; &lt;/div&gt; &lt;label for="period"&gt;Desired Order Period&lt;/label&gt; &lt;select id="period" name="period"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;?php for($i = 0; $i &lt;= 365; $i ++){ $startdate = strtotime("today + $i day"); $enddate = strtotime("today + " . ($i + 6) . " day"); if(date('D', $startdate) == 'Mon'){ echo '&lt;option value="' . date('Y-m-d', $startdate) . '"&gt;' .date('d M y', $startdate) . " to " . date('d M y', $enddate) . "&lt;/option&gt;"; } } ?&gt; &lt;/select&gt; &lt;label&gt;Choose an order type below and provide requested details&lt;/label&gt; &lt;div data-role="page" data-theme="a" id="pagebody"&gt; &lt;div data-role="collapsible-set"&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Auto Spread Order &lt;/h3&gt; &lt;label for="asuom"&gt;Unit of Measure for Order&lt;/label&gt; &lt;select id="asuom" name="asuom"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;option value="Bundles"&gt;Bundles&lt;/option&gt; &lt;option value="Cubes"&gt;Cubes&lt;/option&gt; &lt;option value="Pieces"&gt;Pieces&lt;/option&gt; &lt;/select&gt; &lt;label&gt;Number of Cubes to Autospread&lt;/label&gt; &lt;input type="text" name="ascubes" id="ascubes"&gt; &lt;label&gt;Products over which to spread&lt;/label&gt; must work on this... &lt;input type="submit" name="previous_order" id="previous_order" value="Continue"&gt; &lt;/div&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Previous Order &lt;/h3&gt; &lt;label&gt;Select Order to use as template&lt;/label&gt; &lt;input type="submit" name="previous_order" id="previous_order" value="Continue"&gt; &lt;/div&gt; &lt;div data-role="collapsible"&gt; &lt;h3&gt; Blank Order &lt;/h3&gt; &lt;label for="buom"&gt;Unit of Measure for Order&lt;/label&gt; &lt;select id="buom" name="buom"&gt; &lt;option value="0"&gt;&lt;/option&gt; &lt;option value="cubes"&gt;Bundles&lt;/option&gt; &lt;option value="cubes"&gt;Cubes&lt;/option&gt; &lt;option value="pcs"&gt;Pieces&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" name="blank_order" id="blank_order" value="Continue"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>View 2:</p> <pre><code>&lt;?php $newperiod = date('Y-m-d',strtotime($period) + (24*3600*6)); ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Capture blank Order &lt;/title&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).on('pageshow', '#pagebody', function(){ function callAutocomplete(element) { $(element).autocomplete( { source: "get_sku_codes", messages: { noResults: '', results: function() {} }, select: function( event, ui ) { var selectedObj = ui.item; $.post('get_sku_prices', {data:selectedObj.value},function(result) { $(element).parent().parent().find('input[id^="price"]').val(result); }); } }); } $(function() { var counter = 1; jQuery("table.authors-list").on('change','input[name^="qty"]',function(event) { event.preventDefault(); counter++; var newRow = jQuery('&lt;tr&gt;'+ ' &lt;td&gt;&lt;a class="deleteRow"&gt; &lt;img src="&lt;?php echo base_url() ?&gt;application/assets/images/no.jpg" /&gt;&lt;/a&gt;&lt;/td&gt;' + ' &lt;td&gt;&lt;input type="text" id="product' + counter + '" name="product' + counter + '" /&gt;&lt;/td&gt;' + ' &lt;td&gt;&lt;input type="text" id="price' + counter + '" name="price' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td&gt;&lt;input type="text" id="qty' + counter + '" name="qty' + counter + '" class="linetotal" /&gt;&lt;/td&gt;'+ ' &lt;td&gt;&lt;input type="text" id="total' + counter + '" name="total' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td&gt;&lt;input type="text" id="discount' + counter + '" name="discount' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;td valign=top&gt;&lt;input type="checkbox" id="treated' + counter + '" name="treated' + counter + '" /&gt;&lt;/td&gt;'+ ' &lt;/tr&gt;'); jQuery('table.authors-list').append(newRow); callAutocomplete("#product"+ counter); }); $("#product").autocomplete( { source: "get_sku_codes", messages: { noResults: '', results: function() {} }, select: function( event, ui ) { var selectedObj = ui.item; $.post('get_sku_prices', {data:selectedObj.value},function(result) { $("#product").parent().parent().find('input[id^="price"]').val(result[0]); }); } }); //code to calculate line cost (price*qty) var total=Number($('input[id^="price"]').val()) + Number($('input[id^="qty"]').val()); $('input[id^="total"]').val(total); //code to calculate Grand Total of all line costs (sum of all line totals) }); }); &lt;/script&gt; &lt;/head&gt; &lt;body &gt; &lt;div data-role="page" data-theme="a" id="pagebody"&gt; &lt;div class="wrap-header"&gt; &lt;div data-role="header" data-mini="true" data-ajax="false"&gt; &lt;a data-icon="grid" data-mini="true" data-theme="a" onclick="window.location.href='/pointer'"&gt;Menu&lt;/a&gt; &lt;h3&gt;New Blank Order&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; &lt;div data-role="content"&gt; &lt;center&gt; &lt;table width=80%&gt;&lt;tr&gt;&lt;td&gt; &lt;input type="text" name="customer" id="customer" value="&lt;?php echo $customer; ?&gt;" disabled&gt; &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="period" id="period" value="&lt;?php echo $period." to ".$newperiod; ?&gt;" disabled&gt; &lt;/td&gt;&lt;td&gt; &lt;input type="text" name="buom" id="buom" value="&lt;?php echo $buom; ?&gt;" disabled&gt; &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; &lt;/center&gt; &lt;/div&gt; &lt;table class="authors-list" border=0&gt; &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Product&lt;/td&gt;&lt;td&gt;Price/Cube&lt;/td&gt;&lt;td&gt;Qty&lt;/td&gt;&lt;td&gt;Line Total&lt;/td&gt;&lt;td&gt;Discount&lt;/td&gt;&lt;td&gt;treated&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;a class="deleteRow"&gt; &lt;img src="&lt;?php echo base_url() ?&gt;application/assets/images/no.jpg" /&gt;&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="product" name="product" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="price" name="price" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="qty" name="qty" onchange="fill()" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="total" name="total" class="linetotal" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" id="discount" name="discount" /&gt;&lt;/td&gt; &lt;td valign="top" &gt;&lt;input type="checkbox" id="treated" name="treated" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload