Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JQuery, how do I check if the DOM is ready? [duplicate]

Possible Duplicate:
javascript domready?

I want to check whether $(function(){ }); is "ready".

Return true if DOM is ready, false otherwise

like image 893
TIMEX Avatar asked Dec 04 '11 07:12

TIMEX


2 Answers

From jQuery code

if ( jQuery.isReady ) {  
    fn.call( document, jQuery );
} 

like image 196
Sudhir Bastakoti Avatar answered Oct 05 '22 23:10

Sudhir Bastakoti


You can use the explicit call

$(document).ready(function(){
  // do this after dom is ready
});

Or use the shortcut

$(function(){
  // do this after dom is ready
});

It's also useful to wrap your jQuery in an anonymous function when you're using other libraries; Also very common to use this when writing jQuery plugins.

(function($, window){
  // use $ here freely if you think any other library might have overridden it outside.
  $(function(){
    // do this after dom is ready
  });
})(jQuery, window);

Lastly, you can use jQuery.isReady (bool)

if (jQuery.isReady) {
  // do something
}
like image 36
maček Avatar answered Oct 06 '22 00:10

maček