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

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?


Dave's Answer:

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    

Subscribe!

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

Comments

Hi Dave,
Wish you a very happy new year.
I like the stuff here on this site. its amazing.
I have one query in regard with the above article -
Can we add certain "auto-suggest words" (may b more than 50) in google custom search?

Posted by: Salil Chaudhary at January 4, 2010 7:52 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.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

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!

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
Book Links
© 2002 - 2010 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.