Using Google to host your jQuery (or other) JavaScript libraries
by
, 03-05-2009 at 09:28 PM (184515 Views)
A growing number of scripts here on DD utilize the popular jQuery JavaScript library as its backbone, which means at the top of these scripts you'll see a reference such as:
which obviously requires you to download and host "jquery-1.2.6.pack.js" locally on your server. For those of you who likes to minimize hosting your own files for whatever reason, Google does provide the service of remote hosting of all of the popular JavaScript libraries- including jQuery- in which all you have to do is point your script to access that library on Google's server instead.Code:<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
There are two ways to access jQuery on Google's server.
1) A direct, static reference to the desired jQuery file/ version:
As of writing version 1.3.2 is the latest version of jQuery, so I've chosen to access that particular file. Google hosts previous versions of the library as well unless otherwise noted, such as "1.2.6/jquery.min.js", if you wish to access that instead.Code:<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
In general method 1) is the simplest way to take advantage of Google hosted libraries, and is the approach we will be slowly implementing for future jQuery related scripts here on DD. An example would be the updated Animated Collapsible DIV script.
2) A dynamic reference to the desired jQuery file using Google Ajax API:
This approach lets you reference a version of jQuery more generically, so instead of being locked into a specific version of jQuery (that will become outdated in time), you can, for example, reference the latest build of version 1.3.x (with x always reflecting the latest minor release at the time). Lets start with just referencing version 1.3.2 of jQuery, however:
When accessing jQuery dynamically using Google's Ajax API, you must useCode:<script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("jquery", "1.3.2"); //load version 1.3.2 of jQuery google.setOnLoadCallback(function() { jQuery(function($) { // run your jQuery code in here after DOM has loaded }); }); </script>google.setOnLoadCallback()
instead of the venerablejQuery(document).ready()
function to execute any code once the document's DOM has become available. That's because jQuery is now being dynamically loaded, and only by usinggoogle.setOnLoadCallback()
can you reliably know when jQuery has fully loaded (including access tojQuery(document).ready()
).
Using the Google Ajax API syntax, if you wish to load the latest version of jQuery 1.3.x, you'd just do:
Personally I'm not a fan of this second approach of accessing jQuery on Google. It requires loading Google's Ajax API first (which slows things down a bit), use of the "awkward"Code:google.load("jquery", "1.3"); //load latest version 1.3.x of jQuerygoogle.setOnLoadCallback()
, plus the automatic versioning feature which if used may actually break a jQuery script that's not tested in the latest version.