Possible Duplicate:
javascript domready?
I want to check whether $(function(){ });
is "ready".
Return true if DOM is ready, false otherwise
Possible Duplicate:
javascript domready?
I want to check whether $(function(){ });
is "ready".
Return true if DOM is ready, false otherwise
From jQuery code
if ( jQuery.isReady ) { fn.call( document, jQuery ); }
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
}
That function will only execute when the dom is ready. That's the point of wrapping your code in that function ;). Having said that, some things (like images, deferred scripts, etc.) might not be fully loaded or rendered yet, so be aware.
You can use jQuery.isReady
, but it's undocumented and should be avoided.
If you just need to run something after the DOM is ready, you can just call
$(document).ready(function() {
...
});
as normal – that will run the code immediately if the DOM is ready and wait until it is if not.