Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Glossary using DIV and innerHTML scan

Status
Not open for further replies.

thegentleman

IS-IT--Management
Apr 4, 2001
65
GB
Hi,

I've pieced together the following code from various sources throughout tek-tips but have hit on a couple of problems. The function of this code is to scan the text of a loaded page for specific keywords - when they are found it 'draws' a hidden table with a discription of the word that appears when the user moves their mouse over the word (similar to the Google content adwords function).

Below is the code as it now stands:

Code:
<script language="javascript">

var sHighlightClass, oReg;
var oTran = new Array;
oTran[">"] = "&gt;"
oTran["<"] = "&lt;"
oTran[" "] = "&nbsp;"

function moveElement(element) {
	theElement = document.getElementById(element);
	theElement.style.left = event.clientX + document.body.scrollLeft;
	theElement.style.top = event.clientY + document.body.scrollTop - 40;
}

function openElement(element) {
	theElement = document.getElementById(element);
	theElement.style.visibility = "visible";
}

function closeElement(element) {
	theElement = document.getElementById(element);
	theElement.style.visibility = "hidden";
}

function highlightSearchTerms() {
	sText = "";
	sDesc = "";
	oReg = new RegExp;
	sta = sText.split("|");
	stb = sDesc.split("|");
    
for (var i = 1; i < sta.length; i++) {
	oReg.compile( "(" + sta[i] + ")", "ig" );
	highlightSearchRecursive( document.body, sta[i], stb[i] );
}
}

function highlightSearchRecursive( oDOMNode, STerm, SDescription ) {
	for( var i=0; i< oDOMNode.childNodes.length; i++ ) {
		oChild = oDOMNode.childNodes[i];
		if (oChild.nodeType==3) {    
			sText = oChild.nodeValue;
			if (sText.search( oReg ) > -1) {
				sText= oChild.nodeValue.replace( /^ |[<>]/g, entityConvert );
				oSpan = document.createElement("SPAN");
				oSpan.innerHTML = sText.replace( oReg, "<span class='glossary' onmouseover='openElement(" + i + ");moveElement(" + i + ")', onmouseout='closeElement(" + i + ");'>" + "$&" + "</span>&nbsp;<div id='" + i + "' class='glossaryterm'><table width='200' cellpadding='0' cellspacing='0' border='0'><tr><td bgcolor='#000000'><table width='200' cellpadding='2' cellspacing='1' border='0'><tr><td bgcolor='#ACCAE4'><p><b>" + STerm + "</b></p></td></tr><tr><td bgcolor='#FFFFFF'><p>" + SDescription + "</p></td></tr></table></td></tr></table></div>" );
				oDOMNode.replaceChild( oSpan, oChild );
			}
		}
	else
		highlightSearchRecursive( oChild, STerm, SDescription );
	}
}

function entityConvert( sPart ){
	return( oTran[sPart] );
}

</script>

The two variables sText and sDesc are generated from a MySQL database using PHP. The variables contain the keyword or words and the description for that word respectively. Each keyword or description is seperated by a pipe |.

The code works fine as it is but I have two issues that I need to resolve and so far have had no joy:

1. I only want the code to scan one particular block of text - not the whole page. For example at the moment the code scans titles and footer text but I don't want it to do this. If I place a DIV tag around the block of text I want to scan how can I modify the code above to scan just the text within that DIV tag?

2. If there are a lot of words to scan for (in the sText and sDesc variables), the page loads and then hangs until the scan is complete. When the browser is hanging the user cannot do anything - they cannot scroll or click on any links. Is there any way of stopping the browser from hanging while the javascript runs? The script is called in the <body> tag of the page using the onload() command. Will calling the code elsewhere prevent the browser from hanging? I have seen the Google adwords scan working in realtime and this doesn't prevent the user from doing anything whilst it runs, is there any way to mimic this?

Thank's in advance,

~tg
 
The google adsense/adwords system does not use javascript to detect keywords and determine the adwords content.

The javascript on the page triggers a crawl from the adsense bot (user agent Mediapartners-Google/2.1) which scrapes the page content then delivers the relevant ads. So it's a server-side operation (at Google) and does not use the client machine at all.

Anything you try to do client-side will "hang" the browser.

Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Thanks for clearing that up Chris. At least now I know that it cannot be done I can work on something else. It won't be too much trouble to minimise the number of words the function searches for on any given page - this could be done server-side to speed things up.

Which just leaves the issue of only searching a specific block of text. Any ideas?

~tg
 
Really it will depend on what server-side code that will be running. It's exactly the same operation extract a block of text between markers, parse for the words, add the highlight and insert the javascript function.
Methods will vary as well, depending on whether they are static pages or database driven.

ASP Forum333

PHP Forum434

BTW when I need to extract a block of text I use formatted HTML comments such as
Code:
 <!-- [start] -->text to extract<!-- [/start]-->

Chris.

Indifference will be the downfall of mankind, but who cares?
A website that proves the cobblers kids adage.
Nightclub counting systems

So long, and thanks for all the fish.
 
Your problem is here:
Code:
    highlightSearchRecursive( document.body, sta[i], stb[i] );

Instead, call something like:
Code:
    highlightSearchRecursive( [red]document.getElementById("div2search")[/red], sta[i], stb[i] );

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Chessbot,

Thank you for that - I feel a bit stupid now since I used the getElementById function in the functions above.

Thanks again,

~tg
 
lol... no problem.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top