2

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

Thanks in advance if you have a solution.

3 Answers 3

12
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); } 
Sign up to request clarification or add additional context in comments.

6 Comments

This will throw an error on the first line itself if there is no jQuery defined on the page. You should check for typeof instead.
@ShankarSangoli, correct. Thanks for pointing this out. I have updated my answer.
@ShankarSangoli, in the comments section :-)
+1 Thanks Darin. Any thoughts on the pros/cons of loading locally vs from google server?
Oops. spoke too soon. Unless I explicitly load jQuery with php, I can't get my jQuery slider to work with the code above. I'll troubleshoot it. Might be a document ready issue.
|
1

Load script from Javascript by adding a <script> tag:

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

Comments

0

Try this

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.