X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fajax%2Fxhr.js;h=1f136c343ed6e690136fee095f602a0afbacb729;hb=534dbd660eaefbbc5827b1b61ba384e768562086;hp=97db07951d6b8b653f53e8dd5d56114caf3f616d;hpb=ee22a59129e082a2bb9f5bc0b085191ab6faafc8;p=jquery.git diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index 97db079..1f136c3 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -1,5 +1,22 @@ (function( jQuery ) { +var // #5280: next active xhr id and list of active xhrs' callbacks + xhrId = jQuery.now(), + xhrCallbacks, + + // XHR used to determine supports properties + testXHR; + +// #5280: Internet Explorer will keep connections alive if we don't abort on unload +function xhrOnUnloadAbort() { + jQuery( window ).unload(function() { + // Abort all pending requests + for ( var key in xhrCallbacks ) { + xhrCallbacks[ key ]( 0, 1 ); + } + }); +} + // Functions to create xhrs function createStandardXHR() { try { @@ -13,22 +30,6 @@ function createActiveXHR() { } catch( e ) {} } -var // Next active xhr id - xhrId = jQuery.now(), - - // active xhrs - xhrs = {}, - - // #5280: see below - xhrUnloadAbortInstalled, - - // XHR used to determine supports properties - testXHR, - - // Keep track of isLocal in case it gets removed - // from ajaxSettings later on - protocolIsLocal = jQuery.ajaxSettings.isLocal; - // Create the request object // (This is still attached to ajaxSettings for backward compatibility) jQuery.ajaxSettings.xhr = window.ActiveXObject ? @@ -38,12 +39,9 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ? * Additionally XMLHttpRequest can be disabled in IE7/IE8 so * we need a fallback. */ - ( protocolIsLocal ? - createActiveXHR : - function() { - return createStandardXHR() || createActiveXHR(); - } - ) : + function() { + return !this.isLocal && createStandardXHR() || createActiveXHR(); + } : // For all other browsers, use the standard XMLHttpRequest object createStandardXHR; @@ -69,23 +67,6 @@ if ( jQuery.support.ajax ) { return { send: function( headers, complete ) { - // #5280: we need to abort on unload or IE will keep connections alive - if ( !xhrUnloadAbortInstalled ) { - - xhrUnloadAbortInstalled = 1; - - jQuery(window).bind( "unload", function() { - - // Abort all pending requests - jQuery.each( xhrs, function( _, xhr ) { - if ( xhr.onreadystatechange ) { - xhr.onreadystatechange( 1 ); - } - } ); - - } ); - } - // Get a new xhr var xhr = s.xhr(), handle, @@ -149,7 +130,7 @@ if ( jQuery.support.ajax ) { // Do not keep as active anymore if ( handle ) { xhr.onreadystatechange = jQuery.noop; - delete xhrs[ handle ]; + delete xhrCallbacks[ handle ]; } // If it's an abort @@ -159,7 +140,6 @@ if ( jQuery.support.ajax ) { xhr.abort(); } } else { - // Get info status = xhr.status; responseHeaders = xhr.getAllResponseHeaders(); responses = {}; @@ -185,6 +165,12 @@ if ( jQuery.support.ajax ) { // IE - #1450: sometimes returns 1223 when it should be 204 if ( status === 1223 ) { status = 204; + // Various - #8177: a Not Modified response was received + // yet no conditional request headers was provided + } else if ( status === 304 && + !headers[ "if-modified-since" ] && + !headers[ "if-none-match" ] ) { + status = 200; // Status 0 encompasses several cases } else if ( !status ) { // Cross-domain @@ -196,7 +182,7 @@ if ( jQuery.support.ajax ) { status = 302; } // All same-domain: for local files, 0 is a success - } else if( protocolIsLocal ) { + } else if( s.isLocal ) { status = 200; // Opera: this notifies success for all requests // (verified in 11.01). Patch welcome. @@ -224,10 +210,15 @@ if ( jQuery.support.ajax ) { if ( !s.async || xhr.readyState === 4 ) { callback(); } else { - // Add to list of active xhrs + // Create the active xhrs callbacks list if needed + // and attach the unload handler + if ( !xhrCallbacks ) { + xhrCallbacks = {}; + xhrOnUnloadAbort(); + } + // Add to list of active xhrs callbacks handle = xhrId++; - xhrs[ handle ] = xhr; - xhr.onreadystatechange = callback; + xhr.onreadystatechange = xhrCallbacks[ handle ] = callback; } },