0

I have a case where I need to check if jQuery exists on a page and if it doesn't, I need to load it. I create a script tag and reference the external jQuery library. How do I wait for the library to be loaded on the page before I continue with the rest of the script?

UPDATE:

Heres what it looks like after Gion's suggestion:

if (typeof jQuery === 'undefined') {
    var j = document.createElement('SCRIPT');
    j.type = 'text/javascript';
    j.src = '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
    document.getElementsByTagName('head')[0].appendChild(j);
    j.onload = function () {
        var section = document.createElement('DIV');
        section.setAttribute('style', 'height:300px; width:250px; position: fixed; right: 0px; top: 100px;z-index: 9999; border-style:solid;border-width:1px;');
        section.innerHTML = '<p>steps</p><textarea name="steps"></textarea><p>expected results</p><textarea name="results"></textarea><p><input type="button" name="submit" value="add test case" /></p>';

        document.getElementsByTagName('body')[0].appendChild(section);
    };  
} 

6 Answers 6

6
<script type="text/javascript">
    if(typeof jQuery === 'undefined')
        document.write('<sc'+'ript type="text/javascript" src="jquery.js"></scrip'++'t>');
    window.onload = function(){
        // jquery should be present here
    });
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

why its '<sc'+'ript not '<script ?
to prevent the dom parser from thinking that the string '<script>' is in fact a dom tag
Wont it create conflict if this script execute before jquery is loaded ?
Thanks this works. Just out of curiosity, does it work well with all modern browsers?
@Sethunath window.onload is being triggered only after all the dom elements have been loaded (scripts,imgs,css...) so, in the worst case you'll have 2 jQuery libs loaded, the last one loaded overwriting the first. A conflict may be present only if you try to load different jquery versions and you don't know which one is loaded when you run your script.
1

You can check if the jquery is loaded like this

window.onload = function(){
    if(typeof jQuery == "undefined"){
           // jquery is not available 
    }
}

And if jquery is not loaded I would recommend using a javascript loader like requirejs http://requirejs.org/

Comments

0

You may set it to window.onload

window.onload = function () { alert("It's loaded!") }

Execute Javascript When Page Has Fully Loaded

Comments

0

to inlcude jQuery file, ONLY if needed, you need to check it first

function afterLoad(){
  if(typeof(jQuery)!=='undefined')
    return;

  var element1 = document.createElement(“script”);
  element1.src = “pointToYourjQueryFile.js”;
  element1.type=”text/javascript”;
  document.getElementsByTagName(“head”)[0].appendChild(element1);
}

call this function on window load this way...

<body onload="afterLoad()"?>

Comments

0
window.onload = function(){
    var script,head;
    if(!jQuery){
        script = document.createElement('script');
        document.getElementsByTagName('head')[0].appendChild(script);
        script.onload = function(){
           alert('jQuery loaded');
        }
        script.src = 'path_to_jquery';
    }
}

Comments

0

Modernizr uses this line:

<script>window.jQuery || document.write('<script src="./js/libs/jquery-1.6.2.min.js"><\/script>')</script>

But document.write is regarded as unsafe. Therefore I'd connect it with what have been posted:

if(!window.jQuery)
{
    var element1 = document.createElement(“script”);
    element1.src = “somefile.js”;
    element1.type=”text/javascript”;
    document.getElementsByTagName(“head”)[0].appendChild(element1);
}

On the other hand Google uses:

(function() {
    if(window.jQuery) return;

    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'some.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

Which appends the script to the first tag already existing on the website. If you have at least one <script> on you site, use this one. From what I know it's considered the best solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.