Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Modifying search strings on Google site search?

I saw your answer regarding how to modify a Google search box to only look through a specified subdirectory on my site (see Search only subdirectory in Google) and it's interesting, but where's the actual code? I need something I can cut and paste, please!


Dave's Answer:

You're right. I wimped out last time and wrote about the theory behind constraining a search to a specific subdirectory but deferred actually presenting the script. To go back to the beginning, I addressed a question regarding how to modify the Google site search script I wrote about a while back so that it would only search a specific directory rather than your entire site. You'll want to start here: Add a Google site search box to your Web site, then read How to only search a site subdirectory with Google.

Now, in this set of scripts, I am going to assume that you have already spent some time testing different constraints on Google itself and have figured out the specific element you need for things to work. For this example, I'll use inurl:helpdir, though I don't actually have that subdirectory here on Ask Dave Taylor. Got it? :-)

There are two main ways you can take a search string and tweak it to add a specific field. Well, no, there are three. The first is that you can simply add a small snippet of text where you tell your visitors tip: add 'inurl:helpdir' to search our help files, but that's pretty lame and I certainly don't see it as a viable solution.

The two solutions in my head are either to preload the search string with the additional constraint needed - and automatically position the cursor after that passage in the field when selected, or to append the desired constraint to the search string just before it's passed on to Google itself.

This first one is surprisingly tricky and even my pal Dori Smith, author of some of the very best JavaScript books (notably including JavaScript and AJAX Visual Quickstart and JavaScript Essential Training) had to poke around before she solved it. Here's her solution code:

var tempField = document.getElementById("thisField").value;
document.getElementById("thisField").value = tempField;
document.getElementById("thisField").focus();

The specific text field we'd want to have pre-loaded would need to be called "thisField", as in:

<input type="text" name="thisField" id="thisField" value="inurl:helpdir " />

This is cool - it's the same technique Facebook uses in its status field, which preloads "is " when you go to modify your status - but it's also pretty complicated and doesn't quite solve the problem originally posed.

Fortunately the second solution is not only far easier, but much more useful too, because truth be told, if you want the search to be constrained to just your helpdir (or any other directory or set of directories) you don't want users seeing the constraint or being able to tweak it.

The alternative then is to simply append the constraint (inurl:helpdir) to the text field value just before the user's input is sent off to Google for processing. This is done by tying the append to the onSubmit event handler in the actual form tag.

Here's our original search box, so let's start there:

<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>

What we want to do is to add the event handler, which looks like this:

<form method="get" action="http://www.google.com/search"
onsubmit='document.getElementById("q").value += " inurl:helpdir";'>

<div style="border:1px solid black;padding:4px;width:20em;">
<table border="0" cellpadding="0">
<tr><td>
<input type="text" name="q" size="25" id="q"
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>

That's all there is to this modification of the search box. One new line of code to append the string just before the form data is sent, and a tiny tweak to assign a (necessarily unique on the page) id to the actual input field so that we can access it with the JavaScript method getElementById.

That should solve your problem neatly, I think! Please let us know how it worked out.

Thanks again to Dori Smith, my JavaScript guru pal, and John Minnihan for their help with this entry.


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

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

Comments

Dave,

The last of your methods restricts searches on my blog by a limiting word in URLs, which is great. But ... It also adds the limiting word to searches on the Web generally.

Is there a way to limit only searches on my blog, but not on the Web?

Leslie

Posted by: Leslie Katz at February 2, 2008 9:33 PM

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

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









Remember personal info?


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









Search
Find just the answers you seek from among our 1700+ 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
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 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]