Google’s new AJAX Libraries API
Yesterday Google announced that they are going to serve up some of the top JavaScript libraries on its own content delivery network. This allows developers to hand off the responsibilities of storing and caching these libraries to Google. In the announcement Dion Almaer listed several benefits to having Google host these files:
- “Developers won’t have to worry about getting caching setup correctly, as we will do that for you
- If another application uses the same library (much more likely), they there is a much better chance that it will be already caching on the users machine
- The network and bandwidth of the users systems will not be taxed.”
They currently are hosting five of the more popular libraries jQuery, prototype, script.aculo.us, MooTools and dojo. Depending on how you call the library you can get a specific version or just the most recently available one. If this convention catches on then potential when a new person enters your site they may already have the library cached from a visit to another site.
There are two ways to access the libraries at Google. First, you can access them using the standard <script src=”..”> tag. So to include jQuery you would include the following line in your page:
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js”></script>
Second, you can use Google’s own AJAX API Loader. Here’s the example from the site:
<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>
There is a full set of usage guidelines on the documentation site which further explain how to use the loader. You can specify to what level of the version you want of the library (”1″, “1.1″, or “1.1.2″) and the loader will return the most relevant version.
This is a nice validation of the usefulness of frameworks in developing and will help remove some barriers of entry to site creation. I’m excited to give this a try in my next project. One thing to note though, is the files are hosted at an outside site. Now Google is in the business of continuous uptime, but on the off chance that the service goes down your site could go down with it. Also developers on internal company projects may not be able to access an outside service which would also invalidate this method.
Tagged as CDN, dojo, Google, JavaScript, jQuery, MooTools, prototype, script.acul.us+ Categorized as JavaScript