If you want to reproduce the jQuery's document.ready
event, you can use the onreadystatechange
or DOMContentLoaded
events where applicable:
function domReady () { document.body.className += " javascript"; // ... } // Mozilla, Opera, Webkit if ( document.addEventListener ) { document.addEventListener( "DOMContentLoaded", function(){ document.removeEventListener( "DOMContentLoaded", arguments.callee, false); domReady(); }, false ); // If IE event model is used } else if ( document.attachEvent ) { // ensure firing before onload document.attachEvent("onreadystatechange", function(){ if ( document.readyState === "complete" ) { document.detachEvent( "onreadystatechange", arguments.callee ); domReady(); } }); }
Cross-browser fallback
Internet Explorer 8 supports the readystatechange
event,
which can be used to detect that the DOM is ready. In earlier version
of Internet Explorer, this state can be detected by regularily trying to
execute document.documentElement.doScroll("left");
, as this snippet will throw an error until the DOM is ready.
https://developer.mozilla.org/en-US/docs/Web/Reference/Events/DOMContentLoaded