Loading jQuery from Google CDN

Loading jQuery from the Google CDN repository could seem so simple at first glance, but nothing is simple when you don't know how to do it. The first thing you need to know is the URL where Google stores jQuery. This URL is made up by a base URL, that never changes, the name of the file you want to retrieve (jquery.js for the uncompressed version and jquery.min.js for the minified and compressed version), and a version name that specifies which version of jQuery you want to use. So it is as follows:

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

where version might be 1.3, 1.4, 1.5 or simply 1. In that vein, Google handles jQuery versions in a clever way. For example, if you specify a version such as 1.4, then Google will return the latest stable branch of this version (which is 1.4.4). Conversely, if you want to be more specific, you have also to include the jQuery's branch you want to get, for example 1.3.2 or 1.4.2 (note the three numbers). For example, if you want to retrieve the latest version of the current release of jQuery, you can write:

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

This will retrieve the latest release of jQuery, which is 1.5. You can also fetch it in a more specific way:

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

This will also give you the 1.5 version. Now, suppose that you want an older version of jQuery for backward compatibility reasons with Internet Explorer 6. Here's how you can do:

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

For the sake of completeness, some versions of jQuery are not hosted by Google because of their instability in the wild. Check out this page for more information.

Leave a Reply

Note: Only a member of this blog may post a comment.