Tuesday, 16 May 2017

javascript - Is there a way to check document.ready() if jQuery is not available?










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 element, and no jQuery/other libraries.


Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...