Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How can I add a Google search box to my Web site?

Dave, I keep visiting sites and seeing these cool search boxes that let you either search their entire site or search the entire Google database, all without leaving that page! I want to have something like this and would appreciate your helping me figure out the code needed.

Dave's Answer:

This is a question that's going to result in some HTML listings. You know that, so there's no reason to panic!

First off, the good news. Google itself actually has a nice page offering you HTML code you can just cut and paste onto your own Web pages to produce the search box you seek, and some variants beside. Just check out Google Free.

Instead of relying on the bright sparks at Google, though, let's go through the steps of building our own search box instead, so you'll be able to see how it works.

Also, not to pick on Google because I think the world of those folk, but their HTML samples could be cleaned up a bit, usually, so I've also recast it as proper XHTML rather than somewhat sloppy HTML. Just don't tell Larry and Sergey, okay?

The basic technique involved here is to be able to manipulate one of the variables handed to the Google search engine, a variable called sitesearch. Set it to a null value and you're searching the entire World Wide Web, but set it to a specific domain and it's constrained exactly as if you had typed in the Google special notation site:domain.

In addition to that, you need an input field and a submit button. Put them all together and here's the minimalist Google search form that lets the user alternate between just your site (well, in this case just my site) or the entire Web:

<form method="get" action="http://www.google.com/search">

<input type="text"   name="q" size="31"
 maxlength="255" value="" />
<input type="submit" value="Google Search" />
<input type="radio"  name="sitesearch" value="" />
 The Web
<input type="radio"  name="sitesearch"
 value="askdavetaylor.com" checked /> Ask Dave Taylor<br />

</form>
There are some additional tweaks we can apply to make it a bit more fancy, including changing the radio buttons to a single check box, and aligning things a bit more nicely using a table and some simple CSS:
<form method="get" action="http://www.google.com/search">

<div style="border:1px solid black;padding:4px;width:20em;">
<table border="0" cellpadding="0">
<tr><td>
<input type="text"   name="q" size="25"
 maxlength="255" value="" />
<input type="submit" value="Google Search" /></td></tr>
<tr><td align="center" style="font-size:75%">
<input type="checkbox"  name="sitesearch"
 value="askdavetaylor.com" checked /> only search Ask Dave Taylor<br />
</td></tr></table>
</div>

</form>
Here's how that renders when included on an HTML page:

only search Ask Dave Taylor
If you'd like to use this on your own site, simply change the occurrences of askdavetaylor.com as appropriate.

Not too hard at all!


Related HTML, JavaScript and Web Site Programming articles:
✔   How to Create Predefined Google Image Search Links?
Thanks for the Amazon URL [see Creating Amazon Search Links]. That worked beautifully. In fact, I sent you $5.00 for coffee in thanks....
✔   Can I embed a Facebook search box on my blog site?
I've seen your articles about how to add a Twitter or Google search box on a Web page, but I have a tougher...
✔   Can I use CSS for drop shadows on my blog?
I want to give my site a bit of a facelift and add some neat graphical elements. One of which is drop shadows....
✔   How can I embed interactive photo panoramas on my site/blog?
I read through your blog entry about how to take panoramic photos with iOS 6 and an iPhone 5 and got enthused. I've...
✔   How can I create a Twitter search URL shortcut?
I'd like to add a few Twitter search links to my Web site. Is that possible, or does Twitter prohibit this sort of...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 817

Essienkan Ebong said, on January 3, 2013 6:22 AM:

Man, you should be a prof. your techniques are quite simple and so so easy to deploy. Thanks a lot.

satyendra singh said, on January 5, 2013 5:05 AM:

Thanks for the code.very happy.....

Nutty said, on January 9, 2013 5:03 PM:

Does it take time for the search bar to work, Dave? Because I've added it to my friends website but as i search for something, the search results with nothing.

Regards,
Nut

Edd said, on January 19, 2013 7:05 AM:

Hi,

It all works fine for me when hosted on a web based server but now I have set up a Linux based web server at home and nothing happens. I've tried searching for a solution but can't find anything. Any suggestions?

Edd

Neha said, on January 20, 2013 12:25 AM:

Hi,
I want an html code wherein on my site i type a word in a text box and then say submit.When i click submit it should show me an image of that searched word.And later i have to store that image with a user in the database..Can u help?

steven denger said, on February 15, 2013 8:24 PM:

The code you gave does not work. Neither did googles free one. Why do they offer free stuff that does not work? Goddle's made the picture but did not work. Yours came out all scrambled and with no power. How does one get a search box on one's site that freakin works!

Dave Taylor said, on February 15, 2013 8:44 PM:

That's confusing, Steven, because lots of people have used this exactly code snippet and had it work just fine. Are you sure you copied it into the "source" or "html" view of your page editor?

L.Shimer said, on February 16, 2013 4:29 AM:

Dave you're the man :)
Looks awesome and works like a charm, and i was not expecting the box to have a transparent border, a added plus.
You have to register to be able to view my sites layout but if you want to check it out here is the link: www.lawrenceshimer.com
PS don't worry , no porn or junk on my site and I run Nuke Sentinel and Cloud Flare.
Its just a place a few of my friends and I hang out at (were old school gamers)lol

Nathan McLain said, on February 20, 2013 4:01 PM:

Hi Dave,
We have a page that has many publications listed, so I added the Google search box to the page using the specific URL to just that page. I thought I was being smart until I realized that when you click on a search result it's just going to take you back to the page and not to the specific spot on the page that was searched for. EX) If my name is far down on the page, I search for my name, click on the link, it just takes me back to the top of the page instead of where you can find my name on the page. It's probably wishful thinking, but do you know if there is any way to set up a search like this, where when you click on the link in results it takes you to a specific spot on the page?
Thanks,
Nathan

Jullian said, on February 22, 2013 12:34 PM:

How do i save this, because everytime i add it it appears and then doesn't stay!

shakila.ausaf. said, on February 23, 2013 3:02 AM:

sir,
I get add different company/product but how i can get.
please tell me '
Thank you.

adiat said, on March 9, 2013 3:37 AM:

thanks a lots for your helpfull code.

Abhishek said, on March 18, 2013 2:08 AM:

Works well, thanks. This is the minimum code to have Google Search into your website and its very easy to customize using CSS. Simply great.

Ian Robson said, on March 24, 2013 5:25 PM:

Hi Dave

Just love your article "How can I add a Google search box to my Web site?"

My question: How can I show the result of a search on a custom webpage on my site?


Sam Syrchae said, on March 26, 2013 1:59 PM:

Woooh! There are lots of comment on this article. Nice stuff you have in here Dave. At least you have provided much information for all the webmasters out there especially those beginners who does not know so much yet on designing a site. I am not using google custom search or any google product for my own site search tool but I do appreciate this article.

Michael Stevens said, on April 2, 2013 8:09 AM:

Nice one Dave really helped me out.

Clifford said, on April 7, 2013 7:52 PM:

Thank you Dave. Quick, simple, and EFFECTIVE. Just added it to my website and works like a charm. Added 3 more radio buttons for industry specific sources. Very much obliged.

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, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.