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.