jquery/jquery-mobile — Issue #3726: Ajax load may cause unnecessary network traffic
Author: Azuo
Created: 2012-03-03 16:20:35+00:00
State: open

$.mobile.loadPage uses the following code to pre-parse the HTML content loaded by an Ajax request:

var all = $( "

" ),
...
all.get( 0 ).innerHTML = html;

This will cause all scripts ,css, etc., referenced by the <head> section in the HTML content, to be pre-loaded by the browser (though the browser won't actually execute or apply them until they are inserted into the document DOM tree). Since all scripts and css in the <head> section will be discarded later, these network traffics are obviously unnecessary.

Especially, for a script or css that is generated dynamically by the server (e.g., by respecting the user agent), the network traffic is absolutely not a simple 304 response code...

Maybe the problem can be fixed by replacing the following lines in $.mobile.loadPage:

//workaround to allow scripts to execute when included in page divs
all.get( 0 ).innerHTML = html;
page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();

//if page elem couldn't be found, create one and insert the body element's contents
if( !page.length ){
page = $( "

" + html.split( /<\/?body[^>]*>/gmi )[1] + "

" );
}

with:

//workaround to allow scripts to execute when included in page divs
all.get( 0 ).innerHTML = html.split( /<\/?body[^>]*>/gmi )[1];
page = all.find( ":jqmData(role='page'), :jqmData(role='dialog')" ).first();

//if page elem couldn't be found, create one and insert the body element's contents
if( !page.length ){
page = all.attr("data-" + $.mobile.ns + "role", "page");
// or: page = all.jqmData("role", "page") ?
}