Detect Jquery with regular javascript, if not present load it dynamcally

html, javascript, jquery

Solution

Something like this should work.

EDIT: Code added from above link.

var jQueryScriptOutputted = false;
function initJQuery() {

    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {


        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;

            //output the script (load it from google api)
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    } else {

        $(function() {  
            //do anything that needs to be done on document.ready
        });
    }

}
initJQuery();

Problem

I need the code to use regular javascript to detect whether or not JQuery is present, if not, load JQuery file from google or another website UPDATE Two Working Solutions (just copying and pasting the working code here): From Claudio Redi ``` window.jQuery || document.write("<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'>\x3C/script>") ``` From Rob Darwin ``` var jQueryScriptOutputted = false; function initJQuery() { if (typeof(jQuery) == 'undefined') { if (! jQueryScriptOutputted) { jQueryScriptOutputted = true; document.write("<scr" + "ipt type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></scr" + "ipt>"); } setTimeout("initJQuery()", 50); } } initJQuery(); ```

Original source

Related problems