murvinlai murvinlai - 1 year ago 96
Javascript Question

How to make sure $(document).ready run after jQuery.js is available?

In some cases, because of download speed or other async issue, even I put

after including jquery.js, from time to time, I will get complains about
. blah blah blah.

I don't want to do that:

setTimeout(function() {$(document).ready()... }, 1000);

that seems very lame.

so, is there a smart way or proper way or good way to ensure document.ready is really ready to be called? :)

Answer Source

Denis has provided a link with a good solution. I am using it now.

JavaScript - How do I make sure a jQuery is loaded?

See the answer and explanation in the link.

the solution is:

<script type='text/javascript'>

function runScript() {
    // Script that does something and depends on jQuery being there.
    if( window.$ ) {
        // do your action that depends on jQuery.
    } else {
        // wait 50 milliseconds and try again.
        window.setTimeout( runScript, 50 );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download