Can javascript check jQuery and load it if it is not already present?

I'm just looking for simple javascript that can check if the jQuery library is loaded, and if not, it loads it.

Thanks in advance if you have a solution.

+2
source share
3 answers
if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded => load it:
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
    document.getElementsByTagName('body')[0].appendChild(script);
}
+12
source

Download the script from Javascript by adding the tag <script>:

var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
document.getElementsByTagName("head")[0].appendChild(fileref)
+1
source

try it

if(typeof($) == 'undefined' || typeof(jQuery) == 'undefined'){//JQuery do not exist
    //Load the Jquery from your site or any CDN
}
0
source

All Articles