Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check document.ready() if jQuery is not available? [duplicate]

Tags:

javascript

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.

like image 801
bobobobo Avatar asked Apr 18 '11 17:04

bobobobo


People also ask

How will you make sure that DOM is ready using jQuery?

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.

Can I use jQuery without document ready?

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.

Can I have multiple jQuery document ready () method in a HTML page?

Yes we can do it as like I did in below example both the $(document). ready will get called, first come first served.

How do I know if my document is ready?

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.


1 Answers

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.

like image 51
Šime Vidas Avatar answered Sep 21 '22 08:09

Šime Vidas