Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 482
Patriotec Avatar asked Nov 28 '22 09:11

Patriotec


1 Answers

There are many ways, I like this most only because is the less verbose

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>  
<script>window.jQuery || 
    document.write("<script src='js/jquery-1.7.2.min.js'>\x3C/script>")
</script>   
like image 59
Claudio Redi Avatar answered Dec 09 '22 17:12

Claudio Redi