Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Forked from Nutrox/gist:791176
Created January 27, 2011 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalethedeveloper/799285 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/799285 to your computer and use it in GitHub Desktop.
/**
* 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 );
}
}
@dalethedeveloper
Copy link
Author

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".

@Nutrox
Copy link

Nutrox commented Jan 31, 2011

Thanks for the "interactive" catch, I've adjusted the original code accordingly (just replaced "interactive" with "complete")

@dalethedeveloper
Copy link
Author

Hot sauce! Great snippet, man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment