22

How to check if document is ready (all js files loaded, DOM is ready) through jQuery? Is there any flag?

Facing issues if some of the files are not downloaded completely, and the event is raised by user. I want to check inside event handler.

I am using jQuery, asp.net

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Lalit
  • 4,897
  • 7
  • 32
  • 36
  • 3
    This is one of the first lessons in the average jQuery tutorial. Did you for instance read one? You can find some [here](http://docs.jquery.com/Tutorials). The [main tutorial](http://docs.jquery.com/Tutorials:How_jQuery_Works) handles this already in chapter 2. – BalusC May 12 '10 at 14:30
  • Yes, i am aware of Ready function, but how to check if DOM is Ready? – Lalit May 12 '10 at 14:32
  • 1
    @Lalit: the DOM is `$(document)` in jQuery. – BalusC May 12 '10 at 14:33
  • 1
    @Colin: The question probably has an upvote because it is very clear and asks a very specific question that has not been asked elsewhere on stackoverflow... Why shouldn't it have an upvote.. In fact, I think I'm going to give it a second one. – Robin Day May 12 '10 at 14:41
  • 1
    @Colin no, the OP is not asking for `$(document).ready()`. He is asking for how to check whether the ready event has already taking place. Thus, +1 in support. – Pekka May 12 '10 at 14:58
  • Are you perhaps looking for window.onload? – Olly Hodgson May 12 '10 at 15:07
  • @Olly no, he is not looking for an event, he is looking for a flag. – Pekka May 12 '10 at 15:17
  • This question is very confusing! If you have a button event handler that relies on DOM ready state, you don't need to check the state, just don't even attach the behaviour until DOM ready, by placing the event attachment code inside $(document).ready()... – Mathew May 12 '10 at 15:30

10 Answers10

26

This isn't documented, I don't think, but if you look at the jQuery code for $(document).ready:

// If the DOM is already ready
if ( jQuery.isReady ) {
  // Execute the function immediately
  fn.call( document, jQuery );
} // ...

Thus, for a way that may change, you can use $.isReady or jQuery.isReady.

A better way would be to use $(document).ready in line. e.g.:

function myClickHandler(event) {
  // do stuff

  $(document).ready(function() {
    // Do this immediately if DOM is loaded, or once it's loaded otherwise.
  }
}

Essentially, this uses the ready function as a guard. Instead of an if statement, you use the ready function. Of course, the ready function has the behavior that it will run once the DOM is loaded. If your behavior is only wanting something to happen if it's loaded, but never doing it if the DOM isn't loaded yet, then using the flag above, or setting your own, is the better way to go. Setting your own:

$(document).ready(function() {
  window.domIsReady = true;
}

if (window.domIsReady) {
  // do stuff
}

Creating your own is probably better, since jQuery.isReady doesn't seem to be documented, and therefore probably isn't supported and may be changed at any time.

  • In your last example, why not just // do stuff in the $(document).ready block? – Mathew May 12 '10 at 17:04
  • 1
    He specifically said that he wanted a flag to check in another event handler. The mistake was putting the if (window.domIsReady) standalone -- the use case presented would have that in a different function. Sorry for being unclear. – Antonio Salazar Cardozo May 12 '10 at 17:43
7
$(document).ready(function() {
  // Handler for .ready() called.
});
Gabe
  • 49,577
  • 28
  • 142
  • 181
5

Is this a trick question? :)

What you're looking for is document.ready

Example:

  $(document).ready(function() {

   alert ("document is ready!");

  });
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    Guys, I am aware of Ready function. I have another click event handler of button, i just want to check if all other js files are loaded and DOM is in ready state. – Lalit May 12 '10 at 14:47
  • 1
    @Lalit I see. I do not know of a `ready` flag to check, although I wouldn't be surprised if there was one. If nothing comes up, I would build a `$(document).ready()` function that sets a flag to true: `document_loaded` or something. You could then check that flag. – Pekka May 12 '10 at 14:59
  • @gmcalab does `ready()` return the ready state of the document? I'm not sure. – Pekka May 12 '10 at 15:00
  • @Pekka - Now that you mention it, I'd have to double check that. – Gabe May 12 '10 at 15:15
  • Thanks Pekka, I am using the flag based aproach. – Lalit May 12 '10 at 15:54
3

A simple function call to see if the base is even loaded.

   jQuery(document).ready(function(){ 
      alert("howdy");  
    }); 

alternative, find out if it is loaded (better)

<script type="text/javascript">  
if (typeof jQuery == 'undefined')  
{  
    alert('not loaded'); 
}  
</script>

FOR THE SHORT NUTS OUT THERE: (no typeof needed :)

<script type="text/javascript">  
    if(!this.$) 
    {  
        alert('not loaded'); 
    }  
    </script> 
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
2

http://api.jquery.com/ready/ - example as at the bottom of the page

MartyIX
  • 27,828
  • 29
  • 136
  • 207
1

I think what your after is the:

$(window).load(function(){
   alert("All content has been downloaded to the web browser")  
  }); 

which there i did a blog about not so long ago:

Glenn Packer
  • 212
  • 1
  • 8
1

Jquery has this:

$(document).ready(function(){ 
    alert('document ready here') 
}); 

It's become fairly common practice to wrap any jquery that manipulates the DOM in a $(document).ready() block, to ensure the DOM is all there before proceeding.

Mathew
  • 8,203
  • 6
  • 37
  • 59
0

From the jquery 1.8.x .ready() api page, it seems that binding an event handler function using the following syntax resolves the issue:

$(document).ready(handler);

The 'handle' function will be called, even if the 'ready' event was already fired.

Roei Bahumi
  • 3,433
  • 2
  • 20
  • 19
0

Thought the document.ready code was only triggered when the document and all files where loaded. Never had problems with that myself.

If I had the problem, I would either place a var in all files needed and check if they add up, place all code in one file.

Forgive me for not posting a definitive answer

John
  • 404
  • 4
  • 12
  • Nope, document.ready is fired as soon as the HTML DOM has loaded. onload is fired when everything else (e.g. images) have finished. – Olly Hodgson May 12 '10 at 15:05
0

Are you looking for the native window.onload event by any chance? It's fired when everything on the page (including scripts, images, stylesheets) have loaded.

function doStuff() {
    alert("The DOM and everything inside have loaded");
}
window.onload = doStuff; 

See also: addLoadEvent.

Olly Hodgson
  • 15,076
  • 3
  • 40
  • 60
  • ready != loaded, see [here](http://stackoverflow.com/questions/8396407/jquery-what-are-differences-between-document-ready-and-window-load) – Tim Büthe Mar 15 '13 at 12:51