Skip to content

Commit 62acda8

Browse files
jaubourgdmethvin
authored andcommitted
Adds the abort on unload trick back in since IE9 still exhibits the bug
1 parent f6df030 commit 62acda8

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/ajax/xhr.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@ var xhrSupported = jQuery.ajaxSettings.xhr(),
1111
// Support: IE9
1212
// #1450: sometimes IE returns 1223 when it should be 204
1313
1223: 204
14-
};
15-
14+
},
15+
// Support: IE9
16+
// We need to keep track of outbound xhr and abort them manually
17+
// because IE is not smart enough to do it all by itself
18+
xhrId = 0,
19+
xhrCallbacks = {};
20+
21+
if ( window.ActiveXObject ) {
22+
jQuery( window ).on( "unload", function() {
23+
for( var key in xhrCallbacks ) {
24+
xhrCallbacks[ key ]();
25+
}
26+
xhrCallbacks = undefined;
27+
});
28+
}
29+
1630
jQuery.support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
1731
jQuery.support.ajax = xhrSupported = !!xhrSupported;
1832

@@ -22,7 +36,7 @@ jQuery.ajaxTransport(function( options ) {
2236
if ( jQuery.support.cors || xhrSupported && !options.crossDomain ) {
2337
return {
2438
send: function( headers, complete ) {
25-
var i,
39+
var i, id,
2640
xhr = options.xhr();
2741
xhr.open( options.type, options.url, options.async, options.username, options.password );
2842
// Apply custom fields if provided
@@ -51,6 +65,7 @@ jQuery.ajaxTransport(function( options ) {
5165
callback = function( type ) {
5266
return function() {
5367
if ( callback ) {
68+
delete xhrCallbacks[ id ];
5469
callback = xhr.onload = xhr.onerror = null;
5570
if ( type === "abort" ) {
5671
xhr.abort();
@@ -80,7 +95,7 @@ jQuery.ajaxTransport(function( options ) {
8095
xhr.onload = callback();
8196
xhr.onerror = callback("error");
8297
// Create the abort callback
83-
callback = callback("abort");
98+
callback = xhrCallbacks[( id = xhrId++ )] = callback("abort");
8499
// Do send the request
85100
// This may raise an exception which is actually
86101
// handled in jQuery.ajax (so no try/catch here)

test/data/ajax/unreleasedXHR.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<html>
3+
<head>
4+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
5+
<title>Attempt to block tests because of dangling XHR requests (IE)</title>
6+
<script type="text/javascript" src="../../../dist/jquery.js"></script>
7+
<script type="text/javascript">
8+
window.onunload = function() {};
9+
jQuery(function() {
10+
setTimeout(function() {
11+
var parent = window.parent;
12+
document.write("");
13+
parent.iframeCallback();
14+
}, 200 );
15+
var number = 50;
16+
while( number-- ) {
17+
jQuery.ajax("../name.php?wait=600");
18+
}
19+
});
20+
</script>
21+
</head>
22+
<body>
23+
<!-- empty body -->
24+
</body>
25+
</html>

test/unit/ajax.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ module( "ajax", {
3030

3131
//----------- jQuery.ajax()
3232

33+
testIframeWithCallback( "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)", "ajax/unreleasedXHR.html", function() {
34+
expect( 1 );
35+
ok( true, "done" );
36+
});
37+
3338
ajaxTest( "jQuery.ajax() - success callbacks", 8, {
3439
setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
3540
url: url("data/name.html"),

0 commit comments

Comments
 (0)