Possible Duplicate:
$(document).ready equivalent without jQuery
I know you can use the window.onload event to make functions run, but is there a way for a script to query if the document is ready or not?
Something like
function update() { if( !document.ready() ) // don't do unless document loaded return ; } window.setInterval( 'update();', 100 ) ;
Cannot change the <body> element, and no jQuery/other libraries.
jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.
It is in no way required. All it does is make sure that the DOM is loaded before trying to query for and use the elements inside the DOM (usually after dom is ready and before body. onload fires). You can use jquery perfectly fine without it.
Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.
To check if the document is ready and run some code, you can add an event handler to the DOMContentLoaded event of the document object. The DOMContentLoaded event is fired when the initial HTML document has been fully loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
Here you go:
var tid = setInterval( function () { if ( document.readyState !== 'complete' ) return; clearInterval( tid ); // do your work }, 100 );
Read about the document.readyState
property here. I am not sure if all current browsers implement it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With