Google jQuery CDN

NO LONGER UPDATED: July 2012. This page is no longer indexed prominently in Google, so keeping it up-to-date is not much use to anyone. So consider updates finished. Try this link instead

SUMMARY: Get the latest jQuery here:

http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

To include in your web app, cut-and-paste this:

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”></script>

You can also cut-and-paste one of the following to suck it down:

  • curl -O http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
  • wget http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

With the release of JQuery 1.3, I thought I’d mention the Google JQuery CDN, one of several libraries you can yoink from Google’s Ajax Libraries API. Instead of hosting your own JQuery, you just point your web app to Google’s JQuery:

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script> (Modified version from 1.3. I’ll try and keep this post linking to the latest version.)

Google’s documentation would lead you to believe you need to import Google’s library, and then call google.load(), but it’s unnecessary; just pull in the script directly using the link above. The link is officially documented, so it’s going to stay put for a very, very, long time.

There are also other libraries available on Google’s CDN and elsewhere.

The main point of the CDN is caching. If a user hits 100 JQuery sites, the library will only be downloaded once, and the remaining 99 sites will pull it from the local cache. From an operation cost, you also get to offload the cost of server Javascript to Google. As an added bonus, I also find it easier as a developer to kick off a new project by just linking to the CDN – no need to download and copy the latest version of JQuery.

Great. So when not to use a CDN?

Obviously, whenever you’re working offline. This sometimes occurs as a developer working locally, and with Single Page Applications like TiddlyWiki. (The next “major minor” release of TiddlyWiki, v2.5 is JQuery-powered.)

Another case would be when you can deliver faster than the CDN, and care about that. This might be the case when all users are close to the server. Even then, though, you won’t get the local cache benefit from users who already have the JQuery.

Finally, privacy and security concerns. Using the CDN, you are trusting the CDN to faithfully serve JQuery and relying on no third-parties injecting funniness in between the CDN and your user’s browser. If anything went wrong here, your user could be subject to a world of XSS and CSRF attacks. Or, in a more mundane scenario, the CDN might simply go down, thus breaking your web app. Furthermore, you’re giving the CDN data about your users this way. Although Google doesn’t use cookies, others might; and even if they don’t, the IP number is going to be sent back to them. The referrer – being your website – will also be sent to them, so they can, if they want, track your website’s usage.

For many websites, though, it’s great that we can use a reliable CDN like Google’s to fetch JQuery fast.

Update (11-Feb-2009): In a show of recursive linking, I should fess up as to why I originally sat down to wrote thistwitter message.

Update (erm, mid-2009 sometime): Montana left a comment pointing out you can also drop off the minor numbers to get the latest version. So, for the latest JQuery (since 2.0 doesn’t exist), use http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js. Or, for the latest in the old 1.2 series: http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.min.js.

Update (19-March-2010): Scratch that. @premasagar pointed out to me that the link to just the latest version (until 2.0, i.e. http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js) as opposed to a specific version (http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js) is undesirable; Google will expire the “latest” content after one hour, as opposed to one year for a specific version. This is for good reason of course, as the latest version is subject to change. For this reason, I’ve reverted the link at top to be a specific version, which I’ll endeavour to update upon each new release.

13 thoughts on Google jQuery CDN

Comments are closed.

  1. The only reason that I’m avoiding using Google for this is that they probably do all that tracking that you mention at the end and probably to a higher degree than they will admit. And you have to keep in mind, that every url that has their JS will be sent back to google, including any password protected sites.

  2. Great post. I didn’t know you didn’t need the google.load() function. Thanks a lot! As you know, Deploy* 1.4 will include this, and I will now leave out the load function! Hope your modded Deploy* is working for you.

  3. Haha, I come to this post at least once a week, AT LEAST. I usually come to it about once every other day and I come for the generated tag Google generates haha. And I come to it by searching “jquery CDN”.

    Thanks!

  4. Pingback: n² » Blog Archive » SPARQL AJAX Client Library and Example

  5. @andre no, not every page that every user hits will be sent to google, one page per year from all sites the user visits that use the cdn hosted jQuery will be sent to the user, all other requests will be served from the cache, so nothing at all gets sent to google – the browser never makes a http request for it. unless you are using the ‘latest’ version that is…

  6. its about caching and not tracking. BTW everyone want’s to get into the google index in this or that way. In most of my web apps i also load several jquery plugins. a strategy of reducing request can be “minify” to combine and compress all js files at once (even css files and escape comments to ;-) ). everbody have to find out the best way for personal needs.

  7. It wiil be great if Google stores all Jquery plugins in CDN. Not only jquery.min and jquery.ui. We also use jquery.fancybox, jquery.cluetip and jquery.jwbox. But they are not in Google CDN.

  8. to be more accurate …u should remove http from the reference to JQuery and keep

    src=”//ajax.googleapis.com

    ensures that no issues are encountered if your site is using https

  9. Pingback: Always Load the Latest Version of jQuery in WordPress | kevinleary.net

  10. @ Toby Davies: Not completely true. When your browser first requests jQuery, ajax.googleapis.com sends a Last-Modified HTTP response header. Every modern browser then uses a heuristic to determine whether to request the cached file again and conditionally sends a HTTP request with a If-Modified-Since HTTP header, to which ajax.googleapis.com most of the time will respond with a 304 Not Modified. This is an additional HTTP request the jQuery provider can use for tracking and it can slow down page loading speed almost similar to downloading the jQuery file again. There’s another jQuery CDN at code.jquery.com, which is using similar cache check mechanism: The ETag response header and If-None-Match request header.