Thursday, 10 June 2004

Search Engine Keyword Highlight with Javascript

One issue of Bill Zeller’s Staticize plugin for WordPress, which I have discussed yesterday, is its incompatibility with the Search Hilite plugin. Visitor landed on a cached page via a search engine query would not get keywords highlighted, and one must also turn Search Hilite off so that keyword highlights would not accidentally be cached by Staticize. However, much of the Search Hilite functionality can be duplicated on the client side easily using Javascript. So last night I wrote this Javascript module to simply achieve that.

About

This Javascript can be imported into any HTML page. It would analyse the referrer value for search engine keywords, and then highlight those keywords on the page, by wrapping them around <span class=”hilite”>…</span> tags. Document can then define styles else where to provide visual feedbacks.

Live Demo

Search keyword highlight javascript on the might Google, and click on the link that brings back to this page. Hopefully it is still at the number 1 spot :)

Usage

Add the following line to your HTML document/template. It uses windows.onload event handler to perform highlighting after the document has been fully loaded, so it does not really matter where you put it.

    <script type="text/javascript" src="se_hilite.js"></script>

Then Define the following styles in your CSS stylesheet.

    .hilite { background-color: #ff0; }

If Hilite.style_name_suffix is true, define the follow styles instead.

    .hilite1 { background-color: #ff0; }
    .hilite2 { background-color: #f0f; }
    .hilite3 { background-color: #0ff; }
    .hilite4 ...

Currently it supports queries from AllTheWeb, Google, Lycos, MSN and Yahoo, but it is trivial to add supports for other search engines. The Javascript is configurable so you can easily have it to use a different class name for the highlighted style, or using a unique class name for each search keywords. It would not try to highlight keywords on the whole page, but will try to find DOM element of "content" and "container", before highlighting everything inside document.body. That means it would not only work well with WordPress and Movable Type, but you can also use it with other static web pages that do not utilise a server-side solution.

Download

Compatibility

One disadvantages of using a client side Javascript solution is that if somehow Javascript support is absent, either being intentionally turned off or filtered, or text-mode/old browsers that do not support it, then keyword highlight using this solution would not work. It can also be slow working on its search and replace over a large document.

The following browsers have been tested for full compatibility:

  • Mozilla Firefox 1.0
  • Microsoft Internet Explorer 6.0 SP1

The following browsers are partially working.

  • Safari 1.2.3 (Panther) - extra new lines would be injected into <PRE/> when it is retrieved via .innerHTML. Nor does it allow callback function to be passed into String.replace(), so generating distinctive highlights for each keyword does not work.

The following browsers are known not working due to insufficent Javascript support.

  • Safari 1.0.3 (Jaguar)
  • Mac IE 5.x

History

  • 1.2 (19 August 2005)
    • Fix up the bug when search keyword containing partial match of 'span class="hilite"'.
    • Having an extra boolean flag Hilite.exact, whether the parser should perform exact match. Default to true, i.e. searching for "search" will not match "searching".
    • Minor performance optimisation.
  • 1.1 (24 October 2004)
    • Fixed a bug when multiple consective spaces are present in the query string. (Thanks to Gerardo Robledillo for providing the fix)
    • Create anonymous function for window.onload in case it has already been assigned.
    • Minor speed improvement by reducing copying.
  • 1.0 (4 June 2004)
    • Initial release.
Category: General | 7 Comments

Links to This Article

  1. Wed, 27 July 2005 4:32 am
    СоНоты » Blog Archive » К вам пришли с поисковика…

Comments

1.
Avatar for ZeroByte
Posted by ZeroByte on Tue, 22 June 2004 3:31 am

Stumbled on this by accident. Was doing a google search and your site came up. I just want to compliment you on this pretty innovative idea!


2.
Avatar for Paul H. Belinne
Posted by Paul H. Belinne on Thu, 8 July 2004 3:39 pm

When I look at a cached page in Yahoo! does the highlight color behind the keywords mean anything? Is there a color code or key for reference?


3.
Avatar for Rasmus Hansen
Posted by Rasmus Hansen on Tue, 19 April 2005 9:11 pm

Nice script – but it does not support danish characters such as æøå and ÆØÅ.


4.
Avatar for Sonia
Posted by Sonia on Mon, 16 May 2005 8:46 am

I was wondering if there is a frames version of this?


5.
Avatar for piroli
Posted by piroli on Fri, 13 April 2007 3:01 pm

Hi, it’s a very nice script!
So, right away, I’ve just installed into my web site with a little hack on your original source :)

In my hack, for appling in the multiple targeting elements, I try to add Hilite.addTarget methods.
If you are intereted in, please check with my remodeled source.
http://www.magicvox.net/script/se_hilite.js

Thank you.


6.
Avatar for Paul Jackson
Posted by Paul Jackson on Thu, 23 October 2008 11:52 am

I can’t imagine why someone who searches with Google or some other search engine and wants the results to show up in as many multiple colors as there are words in the search terms used.
It’s totally unreadable.
Trying to find a way to turn it off in Firefox 3 took several minutes of time spent…uselessly.
How in hell does these things change without one changing it manually?


Add a comment

Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.