
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! 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;"> </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;"> </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.
Categorized:
CGI Scripts and Web Site Programming
(Article 7817)
Tagged: google, javascript, site hacking, site search Previous: How can my Google site search just look in a subdirectory? Next: How can I update my Facebook status with Twitter Tweets? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
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!
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 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
Join the List!
Book Links
|