Fine-tuning Custom Google Search
After experimenting with a few approaches for adding a Google search box to Radio Free Blogistan (with the help of my readers), I found a satisfactory method. I'm posting the code in this story format so that I can refer to it in a blog post without actually putting the code out there and screwing up people's aggregators.First of all, I set up a free custom search account with Google (in order to get co-branding on the results page, and because the first example I tried to adapt was done that way).
Google supplies a code example that includes a radio-button option for choosing between searching just the site or searching the whole web. I tweaked it a little without changing the functionality (just the labels on the button and radio-button choices):
<FORM method=GET action=http://www.google.com/custom>
<A HREF=http://www.google.com/search>
<IMG SRC=http://www.google.com/logos/Logo_40wht.gif border=0 ALT=Google align=middle></A>
<INPUT TYPE=text name=q size="10" maxlength=255 value="">
<INPUT type=submit name=sa VALUE="Just Goog It!">
<INPUT type=hidden name=cof VALUE="GIMP:darkblue;LW:380;ALC:red;L:http://blogs.salon.com/0001111/images/rfbanner.jpg;GFNT:lightblue;LC:red;LH:100;AH:left;VLC:gray;S:http://blogs.salon.com/0001111/;GALT:blue;AWFID:ba6ecf243ae3d16f;"><br />
>span class="small">
<input type=radio name=sitesearch value="blogs.salon.com" checked>Search blogs.salon.com
<input type=radio name=sitesearch value="">Search the Web</span><br />
</FORM>
Here's the resulting search form:
The problem with this approach is that it searched blogs.salon.com (all Salon blogs), not just RFB. (It works just fine, though, if your blog's address is at the root of a domain name.) After I whined about this on my blog, Ian Landsman sent me code that will search just Radio Free Blogistan:
<form action="http://www.google.com/search" method="get">
<input type="text" name="q" size="10">
<input type="submit" value="Just Goog It!">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="ie" value="ISO-8859-1">
<input type="hidden" name="as_qdr" value="all">
<input type="hidden" name="q" value="site:salon.com">
<input type="hidden" name="q" value="inurl:0001111">
</form>
The resulting search form does the trick:
The only drawback is that it doesn't include the co-branding and it doesn't offer another crack at searching just RFB after delivering the first page of results. These really aren't big issues and the solution would be satisfactory, but I wanted to see if I could have my cake and eat it too, so I slammed together the two code examples and tried to make a hybrid version that used my custom Google account but still focused on just my site and not all of Salon blogs.
Here's what I came up with:
<form method="get" action="http://www.google.com/custom">
<input type="text" name="q" size="10" maxlength="255">
<input type="submit" name="sa" VALUE="Just Goog It!">
<input type="hidden" name="hl" value="en">
<input type="hidden" name="ie" value="ISO-8859-1">
<input type="hidden" name="as_qdr" value="all">
<input type="hidden" name="q" value="site:blogs.salon.com">
<input type="hidden" name="q" value="inurl:0001111">
<input type="hidden" name="cof" VALUE="GIMP:darkblue;LW:380;ALC:red;L:http://blogs.salon.com/0001111/images/rfbanner.jpg;GFNT:lightblue;LC:red;LH:100;AH:left;VLC:gray;S:http://blogs.salon.com/0001111/;GALT:blue;AWFID:ba6ecf243ae3d16f;">
</form>
Because I didn't know precisely what each element does, I was as conservative as possible, which may mean that there are some unnecessary elements in the above example. This is blind hackery, not sophisticated understanding of Google's format.
Here's the resulting search form:
This one works about as well as can be expected. It may be a little confusing for users trying to search again from the results page (because of the site:blogs.salon.com inurl:0001111 that shows up pre-supplied in the search box and must not be deleted if the next search is to be limited to just RFB), but what do you want for free?
I'm now going to replace the calendar on my home template with this search box and include a link to this story for anyone who wants to take advantage of what I've learned.
Please feel free to steal any of the code, but if you do use the custom-search approach, be sure to set up your own account and use your own logo and color choices for the co-branding effects!
