by, 03-05-2009 at 08:28 PM (146583 Views)
There are two ways to access jQuery on Google's server.
1) A direct, static reference to the desired jQuery file/ version:
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:
google.setOnLoadCallback()instead of the venerable
jQuery(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 using
google.setOnLoadCallback()can you reliably know when jQuery has fully loaded (including access to
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 jQuery
google.setOnLoadCallback(), plus the automatic versioning feature which if used may actually break a jQuery script that's not tested in the latest version.