Tuesday, July 21, 2009

Simple sample code of Google Ajax Search using jQuery

A same javascript code is written often. So, I paste simple a sample code at below. This JS code loads a JSONP of Google Ajax Search by jQuery and dumps titles to Firebug console.


var apiUrl = 'http://ajax.googleapis.com/ajax/services/search/web?';

function googleSearch ( q ) {
var jsonUrl = apiUrl + 'v=1.0&q=' + q + '&start=1&callback=?';
loadJson( jsonUrl );
function loadJson( url ){
$.getJSON( url, function( data ) {
if( data.responseData.cursor.estimatedResultCount > 0 ){
$.each(data.responseData.results, function(i, entry){
console.log(entry.title);
});
}
});
}
}

No comments:

Post a Comment