Search Engine Highlight Javascript 1.3
Further improvement has been made to the Search Engine Highlight Javascript, which has recently been moved from my personal website to FuCoder.com. Here are the changes:
- Rewrite the parsing engine to use DOM instead of resetting the HTML.
- Easier customisation on search engines.
- Use Javascript Cruncher for distribution.
Download the SE-Hilite 1.3 (2.7kb, zip). See usage for instruction.
Previously the highlighting was done by grabbing the entire HTML inside the DOM element, doing some sort of regular expression substitution, and then resetting DOM element with the resulting HTML. It has some issues especially in correctly identifying the text, as well as some regexp magic that behaves differently across browsers.
In SE-Hilite 1.3 it has been changed to use DOM functionality exclusively. A DOM-walker has been implemented to iterate through all text nodes and create/insert <span/> elements for matches. It also makes possible to pause in the middle of a DOM-walk, pass the thread back to browser, and then resume the walk later, so that highlighting large documents can be done progressively without locking up the browser.
Performance wise, a DOM walk is also faster than resetting the HTML attribute of a DOM element. The benchmarking document I used was W3C’s Document Object Model Core at 436kb, highlighting 4 keywords. Under Firefox 1.5beta1/Win32, both methods were on-par, finished highlighting at around 1.6 seconds on a 1.7Ghz Pentium M. Under Internet Explorer 6 SP 1 however, SE-Hite 1.3 finished the test in 3.1 seconds, but the old innerHTML method hangs the browser completely.
I have yet to test the new code on Safari 1.2/2.0, but I think it should work as Safari supports DOM2.
Comments
Packer does both compression and obfuscation, which is not really necessary. If you are just looking at the compression, then they are about the same but I did notice Packer implemented some optimisation like eliminating redundant semi-colons, at the risk of rendering the final script not executable.
There can be more optimisation like rewriting local variables, consolidating literal strings, etc.
I guess I picked Charles Foster’s Javascript Cruncher because I need one now, without actually looking around. I am also thinking of writing a Javascript compressor/obfuscator that does compression on the fly in PHP.
Dear Scott!
Below is a sample results’ URL of the Perl search script I am using on my site:
http://slangsearch.siteburg.com/cgi-bin/search.pl?query=word1+word2+word3&stpos=0&stype=AND#top
What should be the syntax of the “Hilite.search_engines = [...]” parameter in the script so that it would work for my site?
I tried the below setting, but it didn’t work.
Hilite.search_engines = [
[’^http://slangsearch.siteburg.com/cgi-bin/search.pl’,'query=’]
];
Please help.
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.

For no other reason than curiosity, did you compare the Cruncher with Dean Edwards Packer?