|
|
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.
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: 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!
Categorized:
HTML, JavaScript and Web Site Programming
(Article 3802,
Written by Dave Taylor)
Tagged: Previous: Selling jewelry on eBay, does it need to be appraised? Next: Should students use Google for research? Reader Comments To Date: 817satyendra 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, 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, 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 :) Nathan McLain said, on February 20, 2013 4:01 PM:
Hi Dave, 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, 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.
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
Man, you should be a prof. your techniques are quite simple and so so easy to deploy. Thanks a lot.