Making a search go directly to an entry if there is only one result

| No Comments | No TrackBacks
Some of your visitors may know exactly what they are looking for on your blog: that one article you wrote two years ago which had 'bacon sandwich' in the text.  So they type 'bacon sandwich' into your search form, and indeed: one result found!  But now they have to click a link, and that is so much work...

Maybe there is a faster way...
Here is a nifty code snippet you can add to the 'Search Results' template of your Movable Type blog: 

<mt:if tag="searchresultcount" eq="1">
<mt:SearchResults>
<script type="text/javascript">window.location="<mt:entrypermalink>";</script>
</mt:SearchResults>
</mt:if> 

The best place to add this is just before the normal <mt:searchresults> loop. Basically what this does is add a single line of javascript to the output in case there is only one search result. This line of code redirects the browser directly to the article in question. Try it out on this blog: look for 'bacon sandwich' using the search form up there, and you should find yourself back at this article!

No TrackBacks

TrackBack URL: http://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/6

Leave a comment

Recent Comments

  • Nick: Hi, Maarten I'm Nick of Tokyo, Japan. Thank you for read more
  • Sven Baum: In addition to my previous comment where the html was read more
  • Sven Baum: You could also change the display options directly. Edit in read more
  • Sheila Lennon: I have no dog in this race, but our experience read more
  • harrods: Thanks for doing this post! Love it! harrods read more
  • Rob Kenny: Maarten, another Twitter plugin you could use is my HashTag read more
  • Frederic.C: Unfortunatly WebKit is not supporting Colored Syntax on Mac. Safari read more
  • harrods: Hey great post! I was wondering if there is a read more
  • Nicholas Booth: An improvement on using the IP addresses, which may change, read more
  • tiago casagrande: hey, thanks for addressing this. we had major problems with read more
Powered by Movable Type 5.0