Revision [16800]

This is an old revision of SearchHighlighter made by QckJ2l on 2007-05-31 10:49:21.

 

Highlight Searched-for Text


The following code implements a feature that will highlight the search word in the pages that where found by TextSearch or by Google and Yahoo. The core of this feature is a JavaScript library by the name of searchhi.

From the searchhi website:
The searchhi JavaScript library is a way of automatically highlighting words on a page when that page was reached by a search engine. In essence, if you search, for example, Google for some words, and then follow a link from the search results to a searchhi enabled page, the words you searched for will be highlighted on that page.

First I had to make a few minor changes to this JavaScript libary. This to make it aware of the TextSearch activity.
The resulting file (searchhi.js) is then copied in the 3rdparty/plugins/searchhi directory. Also make sure that your create a .htaccess file in the same directory with the following content:
<IfModule mod_rewrite.c>
RewriteEngine off
</IfModule>


Then copy the searchhi.js file into the 3rdparty/plugins/searchhi directory.
%%(javascript)
function highlightWord(node,word) {
Iterate into this nodes childNodes
if (node.hasChildNodes) {
var hi_cn;
for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn ) {
highlightWord(node.childNodes[hi_cn],word);
}
}

And do this node itself
if (node.nodeType
3) { text node
tempNodeVal = node.nodeValue.toLowerCase();
tempWordVal = word.toLowerCase();
if (tempNodeVal.indexOf(tempWordVal) != -1) {
pn = node.parentNode;
if (pn.className != "searchword") {
word has not already been highlighted!
nv = node.nodeValue;
ni = tempNodeVal.indexOf(tempWordVal);
Create a load of replacement nodes
before = document.createTextNode(nv.substr(0,ni));
docWordVal = nv.substr(ni,word.length);
after = document.createTextNode(nv.substr(ni word.length));
hiwordtext = document.createTextNode(docWordVal);
hiword = document.createElement("span");
hiword.className = "searchword";
hiword.appendChild(hiwordtext);
pn.insertBefore(before,node);
pn.insertBefore(hiword,node);
pn.insertBefore(after,node);
pn.removeChild(node);
}
}
}
}

function
SearchHighlight() {
if (!document.createElement) return;
ref = document.referrer;
qs = ref.substr(ref.indexOf('?') 1);
qsa = qs.split('
There are 8 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki