Sunday, November 20, 2005

Add search to any blog

Make sure not to miss the updated information and comments below.

Walter asked for the code to add a search box to his blog. I use a tiny PHP script for universal browser compatibility, but there's a Javascript method that will work on any server.

Somewhere in your blog template, include this code to create the search box. This code is customized for Walter's blog; you just need to replace the two mentions of bikefridaywalter with the URL of your own blog.
<form id="b-search" name="b-search" action="http://search.blogger.com/">
<input type="text" id="b-query" name="as_q" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="hidden" name="bl_url"
value="http://bikefridaywalter.blogdrive.com/" />
<input type="submit" name="Search" id="b-searchbtn"
value="Search BikeFriday Walter"
onclick="document.forms['b-search'].bl_url.value='http://bikefridaywalter.blogdrive.com/'"
/>
</form>
The result of the above code will look like this:


This code is based on the code for the search box in the Blogger.com navbar and uses Google's Blogsearch search engine. JavaScript is usable on any server. The disadvantage is that many people disable JavaScript on their browsers. This code will work for any blog that Google Blogsearch knows about, not just Blogger.com blogs. Note that if you run a blog with Blogger.com and use the Blogger.com navbar, you already have a search box at the top of your blog.

You can get fancy with the interface if you want and search by author, by tag to create a primitive categorization scheme, and so forth.

Update: This code does not work with the Safari browser. Does the Blogger Navbar search box work when you surf with Safari?

Update 2: There's an easier way to do this that's free of Javascript.

8 comments:

  1. Is there any way to make this search through "archived" journal entries also? i.e. my livejournal only displays the 20 most recent entries on the page listed, therefore the search function only searches those 20 entries for the desired string.

    just curious :)

    ReplyDelete
  2. can't get it to work on blogdrive. :( what's the php code?

    speaking of code, here's what you need to get safari to give you a little blue rss button for your feed in the address bar:

    <link rel="alternate" type="application/rss+xml" title="RSS" href="http://bikefridaywalter.blogdrive.com/index.xml" />

    obviously you'll need to replace the url with the appropriate one.

    ReplyDelete
  3. Sarah:
    Livejournal doesn't automatically send pings when you post articles -- hence, Google Blogsearch doesn't know about your Livejournal blog.
    I just sent a ping for your blog and I'll try a search in the morning (if I remember) to see what happens.

    Walter:
    The sample code on the page is specifically for your blog so it should just work by cutting and pasting directly. PHP code requires PHP support -- is there PHP support on Blogdrive?

    ReplyDelete
  4. update: the java code worked in mozilla. i guess safari doesn't like it. oh well. all my fellow mac geeks will be without search capabilities. (and yes, i know we can use camino or explorer or whatever.. but it's so nice to not HAVE to)

    i'm pleased to find that i don't have the problem that sarah does but i also find that it can't find EVERYTHING. for example, i wrote about a bunch of books including tim krabbe's "the rider" and katie alvord's "divorce your car!" a long long time ago. searches for krabbe and divorce pulled up nothing. but it also doesn't just include the stuff on the main page, either, as a search for "book" shows.

    btw, php doesn't work on blogdrive as you suspected.

    thanks again, r.

    ReplyDelete
  5. Here's a good technique to show a search result inside your blog, without jumping to the search engine.
    http://watung.blogspot.com/2006/02/adding-search-to-my-blog.html

    ReplyDelete
  6. short and straight forward...athnx

    ReplyDelete
  7. Hi, used the updated version, but both work great, thanks for the hack and the alternative reference!

    ReplyDelete
  8. Thanks a million for a simple, effective resolution.

    ReplyDelete