Makes sure xhrs are actually aborted on unload in IE. Simplifies active xhrs caching...
authorjaubourg <j@ubourg.net>
Mon, 7 Feb 2011 05:11:52 +0000 (06:11 +0100)
committerjaubourg <j@ubourg.net>
Mon, 7 Feb 2011 05:11:52 +0000 (06:11 +0100)
src/ajax/xhr.js

index b18274c..1f136c3 100644 (file)
@@ -1,5 +1,22 @@
 (function( jQuery ) {
 
 (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 {
 // Functions to create xhrs
 function createStandardXHR() {
        try {
@@ -13,18 +30,6 @@ function createActiveXHR() {
        } catch( e ) {}
 }
 
        } catch( e ) {}
 }
 
-var // Next active xhr id
-       xhrId = jQuery.now(),
-
-       // active xhrs
-       xhrs = {},
-
-       // #5280: see below
-       xhrUnloadAbortInstalled,
-
-       // XHR used to determine supports properties
-       testXHR;
-
 // Create the request object
 // (This is still attached to ajaxSettings for backward compatibility)
 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
 // Create the request object
 // (This is still attached to ajaxSettings for backward compatibility)
 jQuery.ajaxSettings.xhr = window.ActiveXObject ?
@@ -62,23 +67,6 @@ if ( jQuery.support.ajax ) {
                        return {
                                send: function( headers, complete ) {
 
                        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,
                                        // Get a new xhr
                                        var xhr = s.xhr(),
                                                handle,
@@ -142,7 +130,7 @@ if ( jQuery.support.ajax ) {
                                                                // Do not keep as active anymore
                                                                if ( handle ) {
                                                                        xhr.onreadystatechange = jQuery.noop;
                                                                // Do not keep as active anymore
                                                                if ( handle ) {
                                                                        xhr.onreadystatechange = jQuery.noop;
-                                                                       delete xhrs[ handle ];
+                                                                       delete xhrCallbacks[ handle ];
                                                                }
 
                                                                // If it's an abort
                                                                }
 
                                                                // If it's an abort
@@ -152,7 +140,6 @@ if ( jQuery.support.ajax ) {
                                                                                xhr.abort();
                                                                        }
                                                                } else {
                                                                                xhr.abort();
                                                                        }
                                                                } else {
-                                                                       // Get info
                                                                        status = xhr.status;
                                                                        responseHeaders = xhr.getAllResponseHeaders();
                                                                        responses = {};
                                                                        status = xhr.status;
                                                                        responseHeaders = xhr.getAllResponseHeaders();
                                                                        responses = {};
@@ -223,10 +210,15 @@ if ( jQuery.support.ajax ) {
                                        if ( !s.async || xhr.readyState === 4 ) {
                                                callback();
                                        } else {
                                        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++;
                                                handle = xhrId++;
-                                               xhrs[ handle ] = xhr;
-                                               xhr.onreadystatechange = callback;
+                                               xhr.onreadystatechange = xhrCallbacks[ handle ] = callback;
                                        }
                                },
 
                                        }
                                },