Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Google Ajax Search

/skill level/
/viewed/
0 Times

Here is some code you can use to add Google's search functionality to your website. You can add both a standard Google search box that will search the web at large (the first example) or a site-specific search box that will search only pages within a specific URL (the second example). If you want to restrict searches to your own site, use the second example and provide your site's URL as the searchable domain.

You'll need an API key from Google. They are freely available for non-commercial applications.


General Google Search:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>My GAjax Search</title>
		<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=YOURKEYHERE" type="text/javascript"></script>

		<script language="Javascript" type="text/javascript">
    	//<![CDATA[

    	function OnLoad() {
      	// Create a search control
      	var searchControl = new GSearchControl();

      	// create a search object 
      	searchControl.addSearcher(new GwebSearch());
      	// tell Google where to draw the searchbox
      	searchControl.draw(document.getElementById("search-box"));
      
    	}
    	GSearch.setOnLoadCallback(OnLoad);

    	//]]>
		</script>
	</head>
<body>

		<!-- Search box -->

		<div id="search-box">

		</div>

</body>
</html>


Site Specific Google Search:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>My GAjax Site Search</title>
		<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=YOURKEYHERE" type="text/javascript"></script>



		<script language="Javascript" type="text/javascript">
    	//<![CDATA[

    	function OnLoad() {
      	// Create a search control
      	var searchControl = new GSearchControl();

     	// create a search object 
      	var siteSearch = new GwebSearch();
	  	siteSearch.setUserDefinedLabel("YourSite");
	  	siteSearch.setUserDefinedClassSuffix("siteSearch");
	  	siteSearch.setSiteRestriction("example.com");
	  	searchControl.addSearcher(siteSearch);
      	// tell Google where to draw the searchbox
      	searchControl.draw(document.getElementById("search-box"));
      
    	}
    	GSearch.setOnLoadCallback(OnLoad);

    	//]]>
		</script>
	</head>
	<body>

		<!-- Search box -->

		<div id="search-box">

		</div>

	</body>
</html>

Introduction

money,GOD,bible,you need to bleave in JESUS.

Code and Explanation

Suggested readings

Edit this article
Reddit Digg
 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year