]> source.dussan.org Git - jquery.git/commitdiff
Fix #14379: AJAX requests on unload
authorRichard Gibson <richard.gibson@gmail.com>
Mon, 28 Oct 2013 21:40:13 +0000 (17:40 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Mon, 28 Oct 2013 21:53:31 +0000 (17:53 -0400)
src/ajax/xhr.js
test/data/ajax/onunload.html [new file with mode: 0644]
test/unit/ajax.js

index 8e6696b0dd246f7a77d3edd7aa85eee8312d5104..5dbfc646fe0a6e98f8169d3ff7d9605562c51e8f 100644 (file)
@@ -18,18 +18,16 @@ var xhrSupported = jQuery.ajaxSettings.xhr(),
                // #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 = {};
 
+// Support: IE9
+// Open requests must be manually aborted on unload (#5280)
 if ( window.ActiveXObject ) {
        jQuery( window ).on( "unload", function() {
                for ( var key in xhrCallbacks ) {
                        xhrCallbacks[ key ]();
                }
-               xhrCallbacks = undefined;
        });
 }
 
diff --git a/test/data/ajax/onunload.html b/test/data/ajax/onunload.html
new file mode 100644 (file)
index 0000000..2c629f6
--- /dev/null
@@ -0,0 +1,30 @@
+<!doctype html>
+<html>
+<head>
+       <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
+       <title>onunload ajax requests (#14379)</title>
+       <script src="../../jquery.js"></script>
+</head>
+<body>
+       <form id="navigate" action="about:blank"></form>
+       <script>
+               jQuery( window ).on( "unload", function() {
+                       var ajaxStatus;
+                       jQuery.ajax({
+                               url: "../name.html",
+                               async: false,
+                               complete: function( xhr, status ) {
+                                       ajaxStatus = status;
+                               }
+                       });
+                       parent.iframeCallback( ajaxStatus );
+               });
+
+               jQuery(function() {
+                       setTimeout(function() {
+                               document.getElementById( "navigate" ).submit();
+                       }, 0 );
+               });
+       </script>
+</body>
+</html>
index da1f2ce3898c3ea3bf0a5a727ebb3d39d354fe7b..d446d64f1a76b3f688051724ea49c317d5cb9f4d 100644 (file)
@@ -1522,6 +1522,11 @@ module( "ajax", {
                }
        } );
 
+       testIframeWithCallback( "#14379 - jQuery.ajax() on unload", "ajax/onunload.html", function( status ) {
+               expect( 1 );
+               strictEqual( status, "success", "Request completed" );
+       });
+
 //----------- jQuery.ajaxPrefilter()
 
        ajaxTest( "jQuery.ajaxPrefilter() - abort", 1, {