-
-
Save dalethedeveloper/799285 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Works in Chrome, Firefox, MSIE, Opera and Safari. | |
*/ | |
function onDOMContentLoaded( event ) { | |
if( event ) { | |
document.removeEventListener( event.type, onDOMContentLoaded, false ); | |
} | |
document.onreadystatechange = null; | |
// For testing purposes. | |
alert( "DOM Content Loaded" ); | |
} | |
if( document.addEventListener ) { | |
document.addEventListener( "DOMContentLoaded", onDOMContentLoaded, false ); | |
} | |
document.onreadystatechange = function() { | |
if( document.readyState == "interactive" || document.readyState == "complete" ) { | |
onDOMContentLoaded( null ); | |
} | |
} |
Thanks for the "interactive" catch, I've adjusted the original code accordingly (just replaced "interactive" with "complete")
Hot sauce! Great snippet, man.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IE doesn't fire document.readyState as "interactive" while other elements are loading (or some other crazy reason). To get it to respond 100% of the time I had to add "complete".