How to detect if HTML document has fully initialized

Most programmers use the “onload” event to detect if the document has fully loaded, but there’s a flaws relying on this technique. The alternative of detecting is to check the DOM tree of the document.

For Firefox and Opera 9+, use “DOMContentLoaded

if ( document.addEventListener )

document.addEventListener(“DOMContentLoaded”, myFunction, false);

For Internet Explorer,

if  ( document.all && !window.opera ) { //

document.writer( ‘<script type=”text/javascript” id=”contentloadtag” defer=”defer” src=”javascript:void(0)”></script>’);

var contentloadtag = document.getElementById(“contentloadtag”);

contentloadtag.onreadystatechange = function() {

if ( this.readyState == “complete” ) [

myFunction();

}

}

}
Source

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth