tags:

views:

247

answers:

8

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

+4  A: 

Is this a trick question? :)

What you're looking for is document.ready

Example:

  $(document).ready(function() {

   alert ("document is ready!");

  });
Pekka
I really think so :)
MartyIX
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
@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
... use ready() then? What's the confusion?
gmcalab
@gmcalab does `ready()` return the ready state of the document? I'm not sure.
Pekka
@Pekka - Now that you mention it, I'd have to double check that.
gmcalab
Thanks Pekka, I am using the flag based aproach.
Lalit
A: 

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.

MatW
.ready() isn't what the OP is looking for.
Olly Hodgson
Alright, own up! Who downvoted just MY answer to this?! For shame! :)
MatW
+5  A: 
$(document).ready(function() {
  // Handler for .ready() called.
});
gmcalab
+1  A: 

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

MartyIX
+2  A: 

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
A: 

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
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
A: 

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
+7  A: 

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.

Antonio Salazar Cardozo
In your last example, why not just // do stuff in the $(document).ready block?
MatW
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