Makes sure no unload handler is bound when not in IE. Also simplifies the whole "on unload abort" code. Also avoids the declaration of yet another variables in the jQuery main closure for the temporary XHR used to assess support properties.

This commit is contained in:
jaubourg 2011-04-21 18:43:40 +02:00
parent 60cfab3d19
commit a28eadff48

View file

@ -1,21 +1,14 @@
(function( jQuery ) { (function( jQuery ) {
var // #5280: next active xhr id and list of active xhrs' callbacks var // #5280: Internet Explorer will keep connections alive if we don't abort on unload
xhrId = jQuery.now(), xhrOnUnloadAbort = window.ActiveXObject ? function() {
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 // Abort all pending requests
for ( var key in xhrCallbacks ) { for ( var key in xhrCallbacks ) {
xhrCallbacks[ key ]( 0, 1 ); xhrCallbacks[ key ]( 0, 1 );
} }
}); } : false,
} xhrId = 0,
xhrCallbacks;
// Functions to create xhrs // Functions to create xhrs
function createStandardXHR() { function createStandardXHR() {
@ -45,15 +38,13 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
// For all other browsers, use the standard XMLHttpRequest object // For all other browsers, use the standard XMLHttpRequest object
createStandardXHR; createStandardXHR;
// Test if we can create an xhr object // Determine support properties
testXHR = jQuery.ajaxSettings.xhr(); (function( xhr ) {
jQuery.support.ajax = !!testXHR; jQuery.extend( jQuery.support, {
ajax: !!xhr,
// Does this browser support crossDomain XHR requests cors: !!xhr && ( "withCredentials" in xhr )
jQuery.support.cors = testXHR && ( "withCredentials" in testXHR ); });
})( jQuery.ajaxSettings.xhr() );
// No need for the temporary xhr anymore
testXHR = undefined;
// Create transport if the browser can provide an xhr // Create transport if the browser can provide an xhr
if ( jQuery.support.ajax ) { if ( jQuery.support.ajax ) {
@ -136,7 +127,9 @@ if ( jQuery.support.ajax ) {
// Do not keep as active anymore // Do not keep as active anymore
if ( handle ) { if ( handle ) {
xhr.onreadystatechange = jQuery.noop; xhr.onreadystatechange = jQuery.noop;
delete xhrCallbacks[ handle ]; if ( xhrOnUnloadAbort ) {
delete xhrCallbacks[ handle ];
}
} }
// If it's an abort // If it's an abort
@ -197,15 +190,18 @@ if ( jQuery.support.ajax ) {
if ( !s.async || xhr.readyState === 4 ) { if ( !s.async || xhr.readyState === 4 ) {
callback(); callback();
} else { } else {
// Create the active xhrs callbacks list if needed handle = ++xhrId;
// and attach the unload handler if ( xhrOnUnloadAbort ) {
if ( !xhrCallbacks ) { // Create the active xhrs callbacks list if needed
xhrCallbacks = {}; // and attach the unload handler
xhrOnUnloadAbort(); if ( !xhrCallbacks ) {
xhrCallbacks = {};
jQuery( window ).unload( xhrOnUnloadAbort );
}
// Add to list of active xhrs callbacks
xhrCallbacks[ handle ] = callback;
} }
// Add to list of active xhrs callbacks xhr.onreadystatechange = callback;
handle = xhrId++;
xhr.onreadystatechange = xhrCallbacks[ handle ] = callback;
} }
}, },