In my application, I am calling a method for every 1000ms to check the document readyState. Following is the code which I am using,
var succ
If you just want to wait until the document is ready there is no need to keep checking - you can listen for the event:
var whenReady = function(callback) {
if (document.readyState === 'complete') callback(); // check not already loaded prior to this function being called
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback); // for standards compliant browsers (including IE 9+)
else if (document.attachEvent) document.attachEvent('onreadystatechange', callback); // for IE 8
};
whenReady(alert('loaded'));
The only downside of this technique is that it only supports IE 8 and later. Libraries such as JQuery offer better legacy browser support and a cleaner syntax:
$(function() {
// anything here will execute once the dom is ready
});