Skip to content

Commit a28eadf

Browse files
committed
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.
1 parent 60cfab3 commit a28eadf

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/ajax/xhr.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
(function( jQuery ) {
22

3-
var // #5280: next active xhr id and list of active xhrs' callbacks
4-
xhrId = jQuery.now(),
5-
xhrCallbacks,
6-
7-
// XHR used to determine supports properties
8-
testXHR;
9-
10-
// #5280: Internet Explorer will keep connections alive if we don't abort on unload
11-
function xhrOnUnloadAbort() {
12-
jQuery( window ).unload(function() {
3+
var // #5280: Internet Explorer will keep connections alive if we don't abort on unload
4+
xhrOnUnloadAbort = window.ActiveXObject ? function() {
135
// Abort all pending requests
146
for ( var key in xhrCallbacks ) {
157
xhrCallbacks[ key ]( 0, 1 );
168
}
17-
});
18-
}
9+
} : false,
10+
xhrId = 0,
11+
xhrCallbacks;
1912

2013
// Functions to create xhrs
2114
function createStandardXHR() {
@@ -45,15 +38,13 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
4538
// For all other browsers, use the standard XMLHttpRequest object
4639
createStandardXHR;
4740

48-
// Test if we can create an xhr object
49-
testXHR = jQuery.ajaxSettings.xhr();
50-
jQuery.support.ajax = !!testXHR;
51-
52-
// Does this browser support crossDomain XHR requests
53-
jQuery.support.cors = testXHR && ( "withCredentials" in testXHR );
54-
55-
// No need for the temporary xhr anymore
56-
testXHR = undefined;
41+
// Determine support properties
42+
(function( xhr ) {
43+
jQuery.extend( jQuery.support, {
44+
ajax: !!xhr,
45+
cors: !!xhr && ( "withCredentials" in xhr )
46+
});
47+
})( jQuery.ajaxSettings.xhr() );
5748

5849
// Create transport if the browser can provide an xhr
5950
if ( jQuery.support.ajax ) {
@@ -136,7 +127,9 @@ if ( jQuery.support.ajax ) {
136127
// Do not keep as active anymore
137128
if ( handle ) {
138129
xhr.onreadystatechange = jQuery.noop;
139-
delete xhrCallbacks[ handle ];
130+
if ( xhrOnUnloadAbort ) {
131+
delete xhrCallbacks[ handle ];
132+
}
140133
}
141134

142135
// If it's an abort
@@ -197,15 +190,18 @@ if ( jQuery.support.ajax ) {
197190
if ( !s.async || xhr.readyState === 4 ) {
198191
callback();
199192
} else {
200-
// Create the active xhrs callbacks list if needed
201-
// and attach the unload handler
202-
if ( !xhrCallbacks ) {
203-
xhrCallbacks = {};
204-
xhrOnUnloadAbort();
193+
handle = ++xhrId;
194+
if ( xhrOnUnloadAbort ) {
195+
// Create the active xhrs callbacks list if needed
196+
// and attach the unload handler
197+
if ( !xhrCallbacks ) {
198+
xhrCallbacks = {};
199+
jQuery( window ).unload( xhrOnUnloadAbort );
200+
}
201+
// Add to list of active xhrs callbacks
202+
xhrCallbacks[ handle ] = callback;
205203
}
206-
// Add to list of active xhrs callbacks
207-
handle = xhrId++;
208-
xhr.onreadystatechange = xhrCallbacks[ handle ] = callback;
204+
xhr.onreadystatechange = callback;
209205
}
210206
},
211207

0 commit comments

Comments
 (0)