Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How can I create a "search Google" button on my Web page?

Thanks for all the help re: adding a Google search box to web site... BUT, what I really want to do is have a button that when clicked will submit a SPECIFIC querry to google from within my web page. I guess its the equivalent of replacing the querry box input mechanism with a predefined text string... how do I do that? or... maybe just being able to pass parameters to google search... is any of this possible?


Dave's Answer:

This is a fun question and one that's surprisingly easy to solve, believe it or not.

The key idea is that what you want to do is actually run the search that you want and just copy and paste the resultant search page URL.

In addition to working with Google, this will work with just about any Web site that has a search engine where the search results page URL embeds the search term itself (some sites with more sophisticated searches use what's called a "method=post" search, so it's quite a bit harder to reverse-engineer).

So let's say that you want to have a link on your site that offers up a pre-defined search for "iphone case". The first step would be to go to Google.com and actually do that search.

Now, the results in your browser include a complex URL in the browser address bar:

firefox google search iphone case

Pull out the URL by clicking in the address bar, selecting everything there, and then using Edit -> Copy, grab it. Here's what I get:

http://www.google.com/search?q=iphone+case&ie=utf-8&oe=utf-8&aq=t&
rls=org.mozilla:en-US:official&client=firefox-a

Now it turns out that much of the information in the URL is unneeded and you can just use "?q=iphone+case" as the search key but let's say that, unlike me, you don't want to experiment and find that out. So instead you just use the entire long URL.

To create a regular text link with this search results URL, just use the format of "<a href="url">clickable text</a>" with the URL you've copied from the Google results and the "clickable text" being the search term. Like this:

<a href="http://www.google.com/search?q=iphone+case&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a">/a</a>

That'll look like this: Look for iphone case on Google.

Done!

Now, for bonus points, let's see how we can make that a nifty button instead. This is a bit more tricky because we need to send each variable as a separate value so for this I am going to lop off all the unneeded variables. The result is this simple form:

<form method="get" action="http://www.google.com/search">
<input type="hidden" name="q" value="iphone case" />
<input type="submit" value="search for iPhone cases on Google" />
</form>

and the results of this are:

That's it. Hope this makes it clear how you can capture these sort fo searches. Remember, any site where the search results page URL includes the search term can be reengineered this way, too, not just Google.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Wow, this is more simple than I thought. lol.

-Mike

Posted by: Mike Huang at November 12, 2008 2:51 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.