Adding Google AJAX Search APIs to Your Web Application: Learn the Easy Basics to Adding Robust Content to Your Site

Adding Google AJAX Search APIs to Your Web Application: Learn the Easy Basics to Adding Robust Content to Your Site
Page content

The most commonly used Google API seems to be Search, making it the perfect candidate for first in a series on including Google Code in web applications. I introduced this topic in “Should I Try the Google APIs?” found on the Brighthub Google Channel.

You can get to the Google AJAX APIs here. Once there you will see the more popular APIs. We are going to address the Google AJAX Search API (referred to as Search API here) first. Quoting Google, “The Google AJAX Search API lets you put Google Search in your web pages with JavaScript.” (https://code.google.com/apis/ajaxsearch/ 10/15/2009). The Google APIs provide “Web Search”, “Local Search” and “Multimedia Search.” The Search API even allows integration with Google Maps. The Web Search API can locally and globally search the Web, News Services and Blogs.

The easiest way to incorporate search is to use the wizards and API key feature at the Google Code Key Sign-up Page. Please read the sign-up pages found in the link.

Wizards at Work

The Wizards are point & click, copy & paste and practically self explanatory. There are wizards for NewsShow_, Map Search, Video Bar, Video Search, Blog Bar, Book Bar,_ and two versions of a News Bar. Let’s look at including the APIs using HTML and scripting. (Go to Google’s AJAX Search Documentation for more information.) There is an interface available for non-JavaScript instances. That raw interface returns JSON results that can be manipulated in most environments.

The Search API is a JavaScript library that is used to place Google Search in your web applications. One of the keys to implementing any of the Google APIs is to include Google’s API loader. This URL needs to be included right after your </strong> tag within the your <strong><HEAD></strong> section using the following code. <strong><em>Please note that JavaScript is case sensitive.</em></strong></p> <p><strong><script src=https://www.google.com/jsapi</strong></p> <p>**"**<strong>type=“text/Javascript”></script></strong></p> <p><strong><script language="Javascript" type="text/javascript"></strong></p> <p><strong>//<![CDATA[</strong></p> <p><strong>google.load(“search”,“1.0”);</strong></p> <p><strong>…</strong></p> <p><strong></script></strong></p> <p>The parameters in the <em>google.load</em> function indicate the API and version that you are calling. In this case, we are calling the <em>search</em> API version 1. Using the Google Loader allows you to place more than one API on a page or in an application.</p> <p>One thing to remember is that the <em>G</em> prefix is removed from the API name and replaced with the appropriate namespace (e.g. GLocalSearch becomes Google.search.LocalSearch). Once you have loaded the search API, you can call the search option you want with the SearchControl object. This object has seven child objects available: WebSearch, VideoSearch, BlogSearch, NewsSearch, ImageSearch, BookSearch and PatentSearch. Each of these can be called from the LocalSearch child as well. The addSearcher method will add the specific object. The following snippet (adapted from <a href="https://code.google.com/apis/ajaxsearch/documentation/" target="_blank">https://code.google.com/apis/ajaxsearch/documentation/</a> 10/20/2009) shows how to implement a local search with WebSearch and NewsSearch.</p> <h2 id="the-code">The Code</h2> <p><strong><!–</strong></p> <p><strong>Original Copyright (c) 2008 Google Inc.</strong></p> <p><strong>You are free to copy and use this sample.</strong></p> <p><strong>License can be found here:</strong></p> <p><a href="https://code.google.com/apis/ajaxsearch/faq/#license" target="_blank"><strong>https://code.google.com/apis/ajaxsearch/faq/#license</strong></a></p> <p><strong>--></strong></p> <p><strong><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”</strong></p> <p><strong>“<a href="https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd%22%3e" target="_blank">https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></a></strong></p> <p><strong><html xmlns="https://www.w3.org/1999/xhtml"></strong></p> <p><strong><head></strong></p> <p><strong><meta http-equiv="content-type" content="text/html; charset=utf-8"/></strong></p> <p><strong><title>Bruce Chapin’s adaptation of Google HelloWorld sample script

<script src=<strong>https://www.google.com/jsapi</strong> type=“text/javascript”>

Loading

This is a very basic implementation of the Google Search API. See the Developer&rsquo;s Guide for a more detailed explanation, including setting options.

This post is part of the series: Using Google Code

Do you want to bring your web pages to life? Add robust, interactive features to your web pages. This series of how-to articles walks you through adding Google APIs, simply, to your applications. Code samples in HTML & JavaScript included.

  1. Using Google Code: Part 1 - Search
  2. Using Google Code: Pt. 2 - Maps
  3. Using Google Code: Pt. 3 - AJAX Feeds API
  4. Using Google Code: Pt. 4 - Book Search