Skip to content
Permalink
Browse files

Fix #13356. Clean up after load/ready handlers. Close gh-1152.

  • Loading branch information
markelog authored and dmethvin committed Feb 4, 2013
1 parent 8226666 commit f3c776dcc939ef3abe6e6ad11be235c8a61f63cf
Showing with 24 additions and 13 deletions.
  1. +24 −13 src/core.js
@@ -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();
}

0 comments on commit f3c776d

Please sign in to comment.
You can’t perform that action at this time.