Using Popular Ajax Libraries on Google
written by Thura Z
at Thursday, 29 May 2008
Today, Google announced the Hosting Ajax Libraries API for the developers whose are using any of the popular JavaScript frameworks such as
Using the libraries from Google is that you don't need to worry uploading those files to a site or difficult to manage all the different versions, caching shared JavaScript libraries, and instead performing Web applications faster.
You can see below how to use those libraries from Google.
Using the libraries from Google is that you don't need to worry uploading those files to a site or difficult to manage all the different versions, caching shared JavaScript libraries, and instead performing Web applications faster.
You can see below how to use those libraries from Google.
What exactly is the AJAX Libraries API?
We have worked with a subset of the most popular JavaScript frameworks to host their work on the Google infrastructure. The AJAX Libraries API then becomes a content distribution network and loading architecture for these libraries.
We work with the key stake holders for these libraries to make sure that the latest stable versions of their work get into our system as they are released. Once we host a release of a given library, we are committed to hosting that release indefinitely.
You can access the libraries in two ways, and either way we take the pain out of hosting the libraries, correctly setting cache headers, staying up to date with the most recent bug fixes, etc.
The first way to access the scripts is simply be using a standard tag that points to the correct place.
For example, to load Prototype version 1.6.0.2 you would place the following in your HTML:<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>
The second way to access the scripts is via the Google AJAX API Loader's google.load() method.
Here is an example using that technique to load and use jQuery for a simple search mashup:
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>
<script src="http://www.google.com/jsapi"></script>
<script>
// Load jQuery
google.load("jquery", "1");
// on page load complete, fire off a jQuery json-p query
// against Google web search
google.setOnLoadCallback(function() {
$.getJSON("http://ajax.googleapis.com/ajax/services/search/web?q=google&;v=1.0&;callback=?",
// on search completion, process the results
function (data) {
if (data.responseDate.results &&
data.responseDate.results.length>0) {
renderResults(data.responseDate.results);
}
});
});
</script>
You will notice that the version used was just "1". This is a smart versioning feature that allows your application to specify a desired version with as much precision as it needs. By dropping version fields, you end up wild carding a field. For instance, consider a set of versions: 1.9.1, 1.8.4, 1.8.2.
Specifying a version of "1.8.2" will select the obvious version. This is because a fully specified version was used. Specifying a version of "1.8" would select version 1.8.4 since this is the highest versioned release in the 1.8 branch. For much the same reason, a request for "1" will end up loading version 1.9.1.
Note, these versioning semantics work the same way when using google.load and when using direct script urls.
By default, the JavaScript that gets sent back by the loader will be minified, if there is a version supported. Thus, for the example above we would return the minified version of jQuery. If you specifically want the raw JavaScript itself, you can add the "uncompressed" parameter like so:
google.load("jquery", "1.2", {uncompressed:true});
Today we are starting with the current versions of the library, but moving forward we will be archiving all versions from now onwards so you can be sure they are available.
For a full listing of the currently supported libraries, see the documentation.Reference from Google Code - Ajax Search API.