]> source.dussan.org Git - jquery.git/commitdiff
Fix #13356. Clean up after load/ready handlers. Close gh-1152.
authorOleg <markelog@gmail.com>
Tue, 22 Jan 2013 17:58:41 +0000 (21:58 +0400)
committerDave Methvin <dave.methvin@gmail.com>
Mon, 4 Feb 2013 20:34:08 +0000 (15:34 -0500)
src/core.js

index d37056d8ddf7af525870f3a1edc6e039fa9edf02..4115c5eae01fec50c48cc9a4386dbbca891a4fb2 100644 (file)
@@ -74,17 +74,25 @@ var
                return letter.toUpperCase();
        },
 
-       // The ready event handler and self cleanup method
-       DOMContentLoaded = function() {
-               if ( document.addEventListener ) {
-                       document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
-                       jQuery.ready();
-               } else if ( document.readyState === "complete" ) {
-                       // we're here because readyState === "complete" in oldIE
-                       // which is good enough for us to call the dom ready!
-                       document.detachEvent( "onreadystatechange", DOMContentLoaded );
+       // The ready event handler
+       completed = function( event ) {
+
+               // readyState === "complete" is good enough for us to call the dom ready in oldIE
+               if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) {
+                       detach();
                        jQuery.ready();
                }
+       },
+       // Clean-up method for dom ready events
+       detach = function() {
+               if ( document.addEventListener ) {
+                       document.removeEventListener( "DOMContentLoaded", completed, false );
+                       window.removeEventListener( "load", completed, false );
+
+               } else {
+                       document.detachEvent( "onreadystatechange", completed );
+                       window.detachEvent( "onload", completed );
+               }
        };
 
 jQuery.fn = jQuery.prototype = {
@@ -869,18 +877,18 @@ jQuery.ready.promise = function( obj ) {
                // Standards-based browsers support DOMContentLoaded
                } else if ( document.addEventListener ) {
                        // Use the handy event callback
-                       document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
+                       document.addEventListener( "DOMContentLoaded", completed, false );
 
                        // A fallback to window.onload, that will always work
-                       window.addEventListener( "load", jQuery.ready, false );
+                       window.addEventListener( "load", completed, false );
 
                // If IE event model is used
                } else {
                        // Ensure firing before onload, maybe late but safe also for iframes
-                       document.attachEvent( "onreadystatechange", DOMContentLoaded );
+                       document.attachEvent( "onreadystatechange", completed );
 
                        // A fallback to window.onload, that will always work
-                       window.attachEvent( "onload", jQuery.ready );
+                       window.attachEvent( "onload", completed );
 
                        // If IE and not a frame
                        // continually check to see if the document is ready
@@ -902,6 +910,9 @@ jQuery.ready.promise = function( obj ) {
                                                        return setTimeout( doScrollCheck, 50 );
                                                }
 
+                                               // detach all dom ready events
+                                               detach();
+
                                                // and execute any waiting functions
                                                jQuery.ready();
                                        }