Moved unload abort code so that the event is only bound if the xhr transport is used...
[jquery.git] / src / ajax / xhr.js
index 7d71402..26e91ce 100644 (file)
@@ -6,8 +6,11 @@ var // Next fake timer id
        // Callbacks hashtable
        xhrs = {},
 
+       // XHR pool
+       xhrPool = [],
+
        // #5280: see end of file
-       xhrUnloadAbortMarker = [];
+       xhrUnloadAbortMarker;
 
 
 jQuery.ajax.transport( function( s , determineDataType ) {
@@ -21,7 +24,27 @@ jQuery.ajax.transport( function( s , determineDataType ) {
 
                        send: function(headers, complete) {
 
-                               var xhr = s.xhr(),
+                               // #5280: we need to abort on unload or IE will keep connections alive
+                               if ( ! xhrUnloadAbortMarker ) {
+
+                                       xhrUnloadAbortMarker = [];
+
+                                       jQuery(window).bind( "unload" , function() {
+
+                                               // Abort all pending requests
+                                               jQuery.each(xhrs, function(_, xhr) {
+                                                       if ( xhr.onreadystatechange ) {
+                                                               xhr.onreadystatechange( xhrUnloadAbortMarker );
+                                                       }
+                                               });
+
+                                               // Reset polling structure to be safe
+                                               xhrs = {};
+
+                                       });
+                               }
+
+                               var xhr = xhrPool.pop() || s.xhr(),
                                        handle;
 
                                // Open the socket
@@ -53,6 +76,8 @@ jQuery.ajax.transport( function( s , determineDataType ) {
                                try {
                                        xhr.send( ( s.hasContent && s.data ) || null );
                                } catch(e) {
+                                       // Store back in pool
+                                       xhrPool.push( xhr );
                                        complete(0, "error", "" + e);
                                        return;
                                }
@@ -64,10 +89,12 @@ jQuery.ajax.transport( function( s , determineDataType ) {
                                        if ( callback && ( abortStatusText || xhr.readyState === 4 ) ) {
 
                                                // Do not listen anymore
+                                               // and Store back in pool
                                                if (handle) {
                                                        xhr.onreadystatechange = jQuery.noop;
                                                        delete xhrs[ handle ];
                                                        handle = undefined;
+                                                       xhrPool.push( xhr );
                                                }
 
                                                callback = 0;
@@ -130,15 +157,13 @@ jQuery.ajax.transport( function( s , determineDataType ) {
                                                                                        status
                                                                        );
 
-                                                       // Guess response if needed & update datatype accordingly
-                                                       if ( status >= 200 && status < 300 ) {
-                                                               response =
-                                                                       determineDataType(
-                                                                               s,
-                                                                               xhr.getResponseHeader("content-type"),
-                                                                               xhr.responseText,
-                                                                               xhr.responseXML );
-                                                       }
+                                                       // Guess response & update dataType accordingly
+                                                       response =
+                                                               determineDataType(
+                                                                       s,
+                                                                       xhr.getResponseHeader("content-type"),
+                                                                       xhr.responseText,
+                                                                       xhr.responseXML );
                                                }
 
                                                // Call complete
@@ -173,19 +198,4 @@ jQuery.ajax.transport( function( s , determineDataType ) {
        }
 });
 
-// #5280: we need to abort on unload or IE will keep connections alive
-jQuery(window).bind( "unload" , function() {
-
-       // Abort all pending requests
-       jQuery.each(xhrs, function(_, xhr) {
-               if ( xhr.onreadystatechange ) {
-                       xhr.onreadystatechange( xhrUnloadAbortMarker );
-               }
-       });
-
-       // Resest polling structure to be safe
-       xhrs = {};
-
-});
-
 })( jQuery );