<html>
<head>
<title>Keyword Highlight Demo</title>
<script>
//List of keywords to make into links
var keywordList = new Array("yahoo", "google", "tek-tips");
//List of URLs to redirect to
//Make sure they line up with your keywords
var URLList = new Array("[URL unfurl="true"]http://www.yahoo.com",[/URL]
"[URL unfurl="true"]http://www.google.com",[/URL]
"[URL unfurl="true"]http://www.tek-tips.com");[/URL]
//List of target Elements to search for text
var targetList = new Array("docBody1", "docBody3");
//Main search & replace function
function keywordHighlight(){
//Loop through the list of target objects
for(var i = 0; i < targetList.length; i++){
var currTarget = document.getElementById(targetList[i]);
if(currTarget != null){
// get their current HTML
var currHTML = currTarget.innerHTML;
//loop throught the keywords
for(var j = 0; j < keywordList.length; j++){
//create a regular expression for the keyword
var regex = new RegExp(keywordList[j],"gi");
//create an anchor link to replace the keyword with
var linkText = "<a href=\"" +
URLList[j] + "\">" +
keywordList[j] + "</a>";
//do the replace
currHTML = currHTML.replace(regex, linkText);
}
// put the replaced HTML back into the object
currTarget.innerHTML = currHTML;
}
}
}
</script>
</head>
<body onload="keywordHighlight()">
<div id="docBody1">
The yahoo text google here tek-tips should yahoo be google replaced.
</div>
<div id="docBody2">
The yahoo text google here tek-tips should yahoo be google left tek-tips alone.
</div>
<div id="docBody3">
The yahoo text google here tek-tips should yahoo also google be tek-tips replaced.
</div>
</body>
</html>