Google was offering free search modules for websites but they did not allow to modify the look of the results that much before. With the “Google AJAX Search API”, now we have the ability to modify the look of the results. Even better, you can be more creative with the results queries.
In this post I will show you how to create a tabbed search results for your site. First thing to do is to sign-up and get your Google AJAX API.
There is a very useful “Developer’s Guide” and samples of the codes to play around in the playground.
In this blog, I am using the tabbed version of Google Search and here is how I accomplish. (At the time of writing this post, content of the blog was carried from another domain that’s why Google hasn’t indexed the pages yet. So that search results will be empty for this blog for a while.)
There are 3 pieces of code, 1 for the header section of your HTML, 1 for the search box and 1 for the results area.
Code for the header section;
This piece of code has to be in the head section of your HTML before the “</head>” tag.
<script src="http://www.google.com/jsapi?key=YourAPI" type="text/javascript"></script>
<style type="text/css">
#searchResults .gsc-control { width : 500px; }
.search-control { margin: 20px; }
</style>
<script language='Javascript' type='text/javascript'>
//<![CDATA[
// Load Search and Feed APIs
google.load("search", "1");
function OnLoad() {
var controlRoot = document.getElementById("searchResults");
// Create a search control
var searchControl = new GSearchControl();
searchControl.setLinkTarget(GSearch.LINK_TARGET_SELF);
searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);
var searchFormElement = document.getElementById("searchControl");
var drawOptions = new GdrawOptions();
drawOptions.setSearchFormRoot(searchFormElement);
drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
// this is where you create your tabbed search results
// for every URL tab, you have to use the search address and the tab label
// This Blog
var searcher = new GwebSearch();
searcher.setSiteRestriction("http://www.lycie.com");
searcher.setUserDefinedLabel("Lycie");
searchControl.addSearcher(searcher);
// google also allow you to search the entire web
searcher = new GwebSearch();
searcher.setUserDefinedLabel("Web");
searchControl.addSearcher(searcher);
// search all other blogs
searcher = new GblogSearch();
searcher.setUserDefinedLabel("All blogs");
searchControl.addSearcher(searcher);
// draw it
searchControl.draw(controlRoot, drawOptions);
}
google.setOnLoadCallback(OnLoad, true);
//]]>
</script>
There are 3 important things to do in this code.
- Sign up and get your Google AJAX Search API. Put your API key where it says YourAPI in the code.
<script src="http://www.google.com/jsapi?key=YourAPI" type="text/javascript"></script> <style type="text/css">
- CSS styling and tab creation are handled by Google. But if you want to use more styling use the CSS section in the code.
<style type="text/css"> #searchResults .gsc-control { width : 500px; } .search-control { margin: 20px; } </style> - Creating the tabs for the search results. Whenever somebody search my blog, search results will be displayed in 3 tabs, Lycie – results from my site, Web – results from the web and All Blogs – Results from all the blogs on the web.
Here is how I do. For every URL tab you want to create use the code below.
var searcher = new GwebSearch();
searcher.setSiteRestriction("http://www.lycie.com");
searcher.setUserDefinedLabel("Lycie");
searchControl.addSearcher(searcher);
There are 2 things to change in this piece of code in order to create new tabs.
searcher.setSiteRestriction("http://www.lycie.com");
searcher.setUserDefinedLabel("Lycie");
This is where you limit your search results. In other word, you tell Google to display results only from this URL. Here I say, limit this tab’s results for this URL “http://www.lycie.com” and use the label “Lycie”
Limiting your results from an URL means, only results from that URL will be displayed in that tab. This is very useful if you have different sections on your site. Common usage for this is Forums and Galleries. If you have a forum on your site, most likely its URL is www.example.com/forums. By using the code below, you can create another tab just for results from your forums.
searcher.setSiteRestriction("http://www.example.com/forums/");
searcher.setUserDefinedLabel("Forums");
Google does not only allow URL restrictions. Google also allows you to search the entire web for videos, blogs, news, images, books and patents. Check the AJAX API’s Playground for more coding tips.
Code for the search box
Put this into your HTML wherever you want to show your search box.
<div id="searchControl">Loading...</div>
Code for the search results
Put this into your HTML wherever you want to show your search results.
<div id="searchResults">Loading...</div>
That’s it. Enjoy your new search engine.




This is a great tip. Thanks for sharing.
Hi,
I got myself a key from my domain.
I tried to run your code but got no output. I then tried to run your code in the google code playground and got an error when I ran debug on it.
“Syntax error (retrieve_cache?unique_id=ag5zYXZlZGJ5dGhlZ29vZ3ISCxIJU2F2ZWRDb2RlGMWK0gEM,19)”
Not found anything of help on the discussion group.
Wondered if you could help me out with this?
Thanks for lesson.
I dont know why did you get the error, but if you have a new site, google probably hasnt crawled your site yet. that might be the reason why you dont get any search results.
this is the code I am using on this blog and it is working for me.
hi i am a web master, i really need that project you describe. so could you send me that project on my e-mail address? i will highly appreciate if you fulfill my favour.
hi,
thanks for a great explanation. i searched from your search box and found that the search url displays like http://www.lycie.com/?s . is there any way to make the search url like http://www.lycie.com/searchterm ?
Thanks for the simple explanation. Google doesn’t explain their options or methods for anyone that doesn’t already understand them.
hey,
I can’t able to use ur Google search code when i give that code my page warning me as :
to make script tags using the “src” attribute work correctly when using the HTML modul, defer=”defer” has been added after the url
if you have any soluation please send me
Thanks . . .
This was very helpful to me in redesigning a web page to include a Google search. I have been trying so many different pieces of code on the web, and yours has given me what I want. Thanks so much.
But I had one little problem. I have two divisions on the page, one a sidebar and one the main. I have placed the search box in the sidebar and the results appear in the main division. But instead of overwriting the existing text in the main division, as I’ve seen some sites do, the results just push the text down the page, which isn’t a good look. Is there any obvious remedy for this please?
Thanks again.
Hi!
I could not have my own API key as I did not have any online page.
I am currently using google ajax search API to display on my own localhost page, as I’m still in the stage of implementation.
Right now, I can run the search engine by using:
The search box always appear on the top left of a page.
Can you please advise me on how to change the position of the search engine to top right?
Thank you very much.
I have installed Google AJAX search on the e-mjm site but still unable to change the width of display result. The search page is here: http://www.e-mjm.org/search.html (if you search for cancer) there will be 4 results. I would prefer the width of the results to spread 100% of the block not 50%.
I have tried to change css .gsc-results .gsc-webResult {width:100%;} using internal style sheet but the width of the search was not changed.
I am using “siteSearch.setSiteRestriction(“e-mjm.org”);”. Can you please advice me what to do to correct the result display setting. Thanks!
I have resolved the issue by having css for gs-result & gs-webResult width in px instead of %.
thank for your post, but there is a problem that we can’t change style of some elements, example: result is always displayed in a div which has width=300px; .
oh,hi,sorry because don’t read all comment before leave my first comment. thank Kim Teng. by changing width of 2 element you mention, i solved my problem. hi, thank all. Really useful topic.