Easily Embed a Search Feature on Your Web Site

Easily Embed a Search Feature on Your Web Site









I Heart AWeber.com

Do you love AWeber, too?

Wouldn't it be nice if you could add a "search" element to a web site easily, without having to write all that horrible text-searching / database / pattern-matching code?

Well, you can - just get Google to search the site for you! Here's how you can embed a Google Site Search form and button on your web site.

How It Works

In a Google search window (or in Google Toolbar if you have that), try this syntax:

site:www.somesite.com searchwords
            

For example,

site:www.fastechWS.com perl tricks
            

This is asking Google to search for "perl tricks" keywords on the "www.fastechWS.com" site only! You must put a space between the site:URL and the first search word. All the usual google search features work, such as "quotes around phrases", -negative -keywords, etc.

Of course, this only works for data that Google has found already about the web site. If your site is brand new, Google doesn't know about it yet, so you won't get any results. Google is sometimes behind a few days on the latest changes to an existing web site.

Cost

This service is completely free.

How To Integrate That Into a Web Page's Search Box

When you want to make a web page have a "Search" box (with a text input and "go" button), you need 2 things:

1. a little javascript in the HEAD of your web site:

<script type="text/javascript">
<!--
function googleSiteSearch() {
var searchSite = " www.fastechWS.com";
var searchString = document.searchform.searchstr.value;
if (searchString != "") {
searchString = searchString.replace(/ /g,"+");
window.location = 'http://www.google.com/search?q=site%3a' + searchSite + '+' + searchString;
}
return false;
} 
// -->
</script>
            

2. a form somewhere in the BODY of your html, where you want the search box to be:

<form name=searchform onsubmit="return false;">
<p> 
Search <input name="searchstr" size=12> <input type="submit" value="go" onclick="googleSiteSearch();">
</p>
</form>
            

Browser Compatibility

I have tested this in IE6, Firefox, and Safari for Windows; it works fine in all of them.

(I recommend testing stuff in those 4 browsers, for maximum coverage)

Takeaway

Let the experts (Google) do what they do best (Search). If they give you a way to utilize their technology for free, why not use it?



Bookmark and Share


Don't miss the latest web tips and tricks!
Subscribe to our low-volume mailing list:

Privacy Policy

See other tricks:  Web | Unix | Perl | SQL | General


Sample Sites | Customers | Our Team | Contact Us | Tips and Tricks | Tools | Our Network | Home

Copyright © 2006 Fastech Learning LLC, all rights reserved.
Phone toll free 1-866-464-6688, Phoenix Metro area 480-895-6688
Problem with this web site? please let us know