Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to call $(document).ready() from inside a function?

If I use the $(document).ready() handler from within a function, will it still guarantee that the code inside it will only be run if the document is ready, even if the document ready event has occurred well in the past?

like image 683
T Nguyen Avatar asked Aug 22 '11 01:08

T Nguyen


1 Answers

Yes.

From the jQuery ready function source.

// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
    // Handle it asynchronously to allow scripts the opportunity to delay ready
    return setTimeout( jQuery.ready, 1 );
}
like image 58
Rahman Kalfane Avatar answered Oct 06 '22 00:10

Rahman Kalfane