What is the most lightweight way to include the jQuery lib into a page dynamically? I'm working on a page where sometimes it runs a few custom scripts (10 lines) and other times the entire jquery is also needed.
-
you shouldn't have to worry about jquery load times, it's hosted on google and about all the websites uses the same link, so it's already cached on everyone's machine – Khodor Oct 23 '09 at 14:20
7 Answers
Just add a script tag for jQuery when you need it:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.example.com/jquery.js';
document.body.appendChild(script);

- 4,023
- 3
- 27
- 31
Just go ahead and include it.
If you have other pages where you use jQuery then it's probably already cached if they do much on your site (or visit it with any frequency). Use the minified form, though. The logic of using Google applies, but the likelihood of cache is smaller.
W.r.t your comments: How often do you validate the pages on your own site? If your site did get cracked, how soon would you know? If the Google-hosted code was altered, the speed of discovery would be many orders of magnitude higher, and the implications to your site would be relatively small, IMO.

- 6,947
- 10
- 41
- 44
Well once the user has already downloaded JQuery, it's cached on their system, so including it after the first initial download is really trivial. You might as well just include it on the pate that you need it on and not worry about trying to add it into the JS runtime later on during the page.

- 113,795
- 27
- 197
- 251
Include it from Google's CDN:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
There's a big chance your clients will already have jQuery cached in their browsers.

- 1,023,142
- 271
- 3,287
- 2,928
-
2I don't trust Google with this. I think this practices is a huge security issue... and I'm waiting for the rest of the webdev world to wake up and smell the coffee. I'd like to serve the files myself. I trust me. – BuddyJoe Oct 23 '09 at 14:26
-
1I wonder if you could write code to check the contents of the jQuery library pulled from google and compute a checksum or something. Of course, a rogue version of jQuery could potentially corrupt the Javascript environment sufficiently that you couldn't trust your checksum code anyway. – Pointy Oct 23 '09 at 14:30
-
1I admit I trust Google more than most companies. But think about all the companies that offer "share this" widgets and people include their scripts on their pages. I don't trust these random companies at all. I someone hacks one of these sites they can inject any script on to your page.... for Phishing or redir you to an exploit etc...... – BuddyJoe Oct 23 '09 at 14:36
-
There needs to be a larger discussion in the web community about a new standard to sandbox some of these 3rd party scripts. I think I have heard other people talk about this in terms of Eval-ing JSON. (similar issues) – BuddyJoe Oct 23 '09 at 14:38
-
new browsers have built in json decode thats not just eval. much better. but like everything on the web, there'll always be the holdouts (i'm looking at you, IE). – Justin Oct 23 '09 at 16:03
Yeah, I'd just include it always - its small, and if you use the google cdn it'll hopefully already be cached.
If you MUST load it on demand, you can write the code for appending a script tag to the body. This code is fairly common.
void((function(){
var e=document.createElement('script');
e.setAttribute('type','text/javascript');
e.setAttribute('src','jquery.js');
document.body.appendChild(e)
})());

- 5,029
- 1
- 21
- 21
-
-
note the code above is suited for inserting ~anywhere~ - its actually from a bookmarklet. Adjust to your needs. – Justin Oct 23 '09 at 16:04
May be you'll find Google AJAX APIs usefull. You can call load jQuery whenever you need by calling:
google.load("jquery", "1");

- 28,143
- 8
- 66
- 82
<script src="http://www.google.com/jsapi"></script>
<script>
//your awesome google
google.load("jquery", "1");
</script>
i have no clue why these guys are saying that google is a security issue and they dont want google to serve it. yall are wrong (or give me reasons why) id say its more likely for you to download a bad or changed version of jquery (you should alwase go to the site) than google giving you issues. googles awesome and they make it easy, host if for you and prolly have more throughput than you weak webserver so why wouldnt you use it?
sorry for dup code im still getting started on this site but i cant find reply button

- 918
- 9
- 16
-
I don't want to trust other webservers that's all. I won't allow a 3rd Party to inject script on to my pages. Even if I think I know what they are injecting. Because they can (1) move it (2) change it (3) they could get hacked and then do anything they want with my page. I realize with Google this is a little less likely. But why not just go the safe route. – BuddyJoe Oct 26 '09 at 17:02