Wednesday, May 28, 2008

Google AJAX Libraries API with Working Example

As is all over the blogosphere (I got it from Google Operating System), Google has decided to streamline AJAX development a bit further by hosting several AJAX libraries on Google servers, and also make them available for loading through a javascript call.

I decided to try it out, but unfortunately the example given by Dion Almaer is incomplete and also has a number of typos (both in the Google blog post and on the AJAX Libraries API home page).

Below is a working example - simply copy to a text file, rename as .htm/.html and run in your favorite browser:

<title>Google Ajax API Test</title>
<script src=""></script>
  // Load jQuery
  google.load("jquery", "1", {uncompressed:true});
  // on page load complete, fire off a jQuery json-p query against Google web search
  google.setOnLoadCallback(function() {
      // on search completion, process the results
      function (data) {
  if (data.responseData.results &&
            data.responseData.results.length>0) {
    //render each result
       function(i, result) {
      //hacked together jquery expression
      $("<p/>").html(i+1 + ". <a href='" + result.url + "'>" + result.title + "</a>: " + result.content).appendTo("#results");
<div id="results"></div>

Labels: , , , , ,


Post a Comment

<< Home