Free tech support / small logo


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.









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

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

-Mike

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

Wow Thats easy....

What would I need to add to make it open in a new Window I Use Safari.

Posted by: Jim Waugh at December 3, 2010 2:46 AM

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

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











Remember personal info?


Please note that I will never send you any unsolicited 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.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 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.