GOOGLE: Playing with Google AJAX Search API

G

I’ve been playing with the Google AJAX Search API for a few hours and it seems pretty easy. (The API isn’t new or anything, it’s just that I haven’t played with it until now.)

The advantage of the AJAX search is that results are presented in the page with no obvious refresh or having to open a new page.

You can see a simple implementation on my home page (just type something in the search box, click Search, and the search results will appear in the page) and also a test search page I built (you can view source on it to see how simple the code is).

Getting started is simple, and there are plenty of example scripts in the Developer Guide. You’ll need an API key, but that is easily obtained (and all free of course).

I won’t go through the code here as it is all well explained in the doco, but I will highlight three little tips that took me a little while to find.

1. Opening search result links in the same page (instead of firing off a new instance)

To do this simply set the Link Target using this code:

searchControl.setLinkTarget( google.search.Search.LINK_TARGET_SELF );

2. Showing all results by default

Set this line of code:

searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);

3. Setting the width of the results set

You need to override the .gsc-control style. In your own stylesheet do something like this:

#searchResults .gsc-control { width : 554px; }

where searchResults is the name of your results DIV

There’s plenty of online help, especially the Google AJAX Search API Group. I’ve only done simple stuff and limited it to Web searches that search only within my web site. You can add tab groups, with local searches, news, blog, book and video searches as well.

Note: The Search results still include sponsored links, but are displayed at the bottom.

Technorati Tags: Google, AJAX Search API

Add comment

By Craig Bailey

Archives