kenneth koontz
kenneth koontz

Reputation: 869

load jQuery library after page has initially loaded

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);
    };  
} 

Upvotes: 0

Views: 2504

Answers (6)

Michał Miszczyszyn
Michał Miszczyszyn

Reputation: 12711

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.

Upvotes: 0

Joseph
Joseph

Reputation: 119847

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';
    }
}

Upvotes: 0

Andreyco
Andreyco

Reputation: 22872

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()"?>

Upvotes: 0

gion_13
gion_13

Reputation: 41533

<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>

Upvotes: 6

Sethunath K M
Sethunath K M

Reputation: 4761

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/

Upvotes: 1

Rami Alshareef
Rami Alshareef

Reputation: 7150

You may set it to window.onload

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

Execute Javascript When Page Has Fully Loaded

Upvotes: 0

Related Questions