mirror of
https://github.com/jquery/jquery.git
synced 2026-04-20 03:01:22 -04:00
Fix #13356. Clean up after load/ready handlers. Close gh-1152.
This commit is contained in:
37
src/core.js
37
src/core.js
@@ -74,16 +74,24 @@ var
|
||||
return letter.toUpperCase();
|
||||
},
|
||||
|
||||
// The ready event handler and self cleanup method
|
||||
DOMContentLoaded = function() {
|
||||
// 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", 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 );
|
||||
jQuery.ready();
|
||||
document.removeEventListener( "DOMContentLoaded", completed, false );
|
||||
window.removeEventListener( "load", completed, false );
|
||||
|
||||
} else {
|
||||
document.detachEvent( "onreadystatechange", completed );
|
||||
window.detachEvent( "onload", completed );
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user