
Can I automatically add words to a Google search?I have a Google search box on my site, thanks to your article How to add Google Search to Your Site, but want to automatically add a few words to the search string that the user enters. Is that even possible? If so, how do I modify the JavaScript code to make it work? Great question, and one that's come up quite a few times since I wrote that original Google search code. Seems like it's popular to not only have a search box on your site, but to tweak the actual search that you send to Google. Who knew? :-) Accomplishing this task, however, is quite a bit more complicated because what we have to do is actually tweak the form itself so that the word or words are grafted onto the search after the user has clicked the submit button but before the data is actually sent back to Google. I asked a developer friend of mine, Luke Kingland, to help out and he describes his solution thusly: "Instead of the user entering search terms directly into the "q" field that Google will read, we're going to have the user first enter their search terms into a temporary field named "custom_terms". The JavaScript that is then run when the user clicks "Submit" assembles the value for the "q" field (which is hidden) by setting it to both what the user typed in and the value of "additional_terms", which the site owner hard codes on the page." Makes sense to me. Let me show you his solution. The first thing is that we now need a JavaScript function to assemble the different elements and send along the form itself. That needs to appear within the "<head>" of your Web page and looks like this: <script>
function runsearch( form ) { // extra terms to include on every search var additional_terms = "wiki"; // terms the user entered var entered_terms = document.getElementById( "custom_terms" ).value; // set the search field to be both custom terms and additional terms var all_terms_field = document.getElementById( "all_terms" ); all_terms_field.value = ""+entered_terms+" "+additional_terms; // submit the form; form.submit(); } </script> That's the complicated part and I've marked in bold where you'd put in the word or words (use a "+" rather than a space if you specify a phrase) you want to have automatically added to the search. Well, the form gets a bit of a tweak too: <form id="search_form"
method="get" action="http://www.google.com/search" onsubmit="runsearch(this);return false;"> <input type="hidden" name="q" id="all_terms" /> <input type="text" id="custom_terms" class="display" name="display" size="25" /> <input type="submit" class="submit" value="Google Search" /> <div class="footer"> <input type="checkbox" name="sitesearch" value="askdavetaylor.com" checked="checked" /> <span> only search Ask Dave Taylor</span> </div> </form> What's it look like? Let's check it out. I'll automatically add "help" to any search you enter below: Cool, isn't it? Hope that's helpful to you! Thanks to Luke Kingland over at Sigrun Labs for his help with this coding project.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
CGI Scripts and Web Site Programming
(Article 9206)
Tagged: google, javascript, web site programming Previous: How do I review an iPhone application for the App Store? Next: Review: Popcap's "Chuzzle" iPhone game Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Hi Dave, I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|