Skip to content

Commit

Permalink
Adds the abort on unload trick back in since IE9 still exhibits the bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jaubourg authored and dmethvin committed Jan 7, 2013
1 parent f6df030 commit 62acda8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/ajax/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@ var xhrSupported = jQuery.ajaxSettings.xhr(),
// Support: IE9
// #1450: sometimes IE returns 1223 when it should be 204
1223: 204
};

},
// Support: IE9
// We need to keep track of outbound xhr and abort them manually
// because IE is not smart enough to do it all by itself
xhrId = 0,
xhrCallbacks = {};

if ( window.ActiveXObject ) {

This comment has been minimized.

Copy link
@jdalton

jdalton May 6, 2013

Member

Has this been resolved in IE10? If so the detection should be updated to avoid the "unload" event.

jQuery( window ).on( "unload", function() {
for( var key in xhrCallbacks ) {
xhrCallbacks[ key ]();
}
xhrCallbacks = undefined;
});
}

jQuery.support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
jQuery.support.ajax = xhrSupported = !!xhrSupported;

Expand All @@ -22,7 +36,7 @@ jQuery.ajaxTransport(function( options ) {
if ( jQuery.support.cors || xhrSupported && !options.crossDomain ) {
return {
send: function( headers, complete ) {
var i,
var i, id,
xhr = options.xhr();
xhr.open( options.type, options.url, options.async, options.username, options.password );
// Apply custom fields if provided
Expand Down Expand Up @@ -51,6 +65,7 @@ jQuery.ajaxTransport(function( options ) {
callback = function( type ) {
return function() {
if ( callback ) {
delete xhrCallbacks[ id ];
callback = xhr.onload = xhr.onerror = null;
if ( type === "abort" ) {
xhr.abort();
Expand Down Expand Up @@ -80,7 +95,7 @@ jQuery.ajaxTransport(function( options ) {
xhr.onload = callback();
xhr.onerror = callback("error");
// Create the abort callback
callback = callback("abort");
callback = xhrCallbacks[( id = xhrId++ )] = callback("abort");
// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
Expand Down
25 changes: 25 additions & 0 deletions test/data/ajax/unreleasedXHR.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Attempt to block tests because of dangling XHR requests (IE)</title>
<script type="text/javascript" src="../../../dist/jquery.js"></script>
<script type="text/javascript">
window.onunload = function() {};
jQuery(function() {
setTimeout(function() {
var parent = window.parent;
document.write("");
parent.iframeCallback();
}, 200 );
var number = 50;
while( number-- ) {
jQuery.ajax("../name.php?wait=600");
}
});
</script>
</head>
<body>
<!-- empty body -->
</body>
</html>
5 changes: 5 additions & 0 deletions test/unit/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module( "ajax", {

//----------- jQuery.ajax()

testIframeWithCallback( "XMLHttpRequest - Attempt to block tests because of dangling XHR requests (IE)", "ajax/unreleasedXHR.html", function() {
expect( 1 );
ok( true, "done" );
});

ajaxTest( "jQuery.ajax() - success callbacks", 8, {
setup: addGlobalEvents("ajaxStart ajaxStop ajaxSend ajaxComplete ajaxSuccess"),
url: url("data/name.html"),
Expand Down

0 comments on commit 62acda8

Please sign in to comment.