Hi all,
I have this snippet of code that highlights words on one of my pages:
//<script language="JavaScript">
function doHighlight(spanText, searchTerm, highlightStartTag, highlightEndTag) {
// the highlightStartTag and highlightEndTag parameters are optional
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = "<font style='background-color:yellow;'>";
highlightEndTag = "</font>";
}
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcSpanText = spanText.toLowerCase();
while (spanText.length > 0) {
i = lcSpanText.indexOf(lcSearchTerm, i + 1);
if (i < 0) {
newText += spanText;
spanText = "";
} else {
// skip anything inside an HTML tag
if (spanText.lastIndexOf(">", i) >= spanText.lastIndexOf("<", i)) {
// skip anything inside a <script> block
if (lcSpanText.lastIndexOf("/script>", i) >= lcSpanText.lastIndexOf("<script", i)) {
// skip anything inside a <HyperLink> block
if (lcSpanText.lastIndexOf("/asp:HyperLink>", i) >= lcSpanText.lastIndexOf("<asp:HyperLink", i)) {
// skip anything inside a <linkbutton> block
if (lcSpanText.lastIndexOf("/asp:linkbutton>",
i) >= lcSpanText.lastIndexOf("<asp:linkbutton", i)) {
// skip anything inside a <Checkbox> block
if (lcSpanText.lastIndexOf("/asp:Checkbox>",
i) >= lcSpanText.lastIndexOf("<asp:Checkbox", i)) {
// skip anything inside a <panel> block
if
(lcSpanText.lastIndexOf("/asp:panel>", i) >=
lcSpanText.lastIndexOf("<asp:panel", i)) {
// skip anything inside a <legend> block
if
(lcSpanText.lastIndexOf("legend>", i) >=
lcSpanText.lastIndexOf("<legend", i)) {
newText += spanText.substring(0, i) + highlightStartTag +
spanText.substr(i, searchTerm.length) + highlightEndTag;
spanText = spanText.substr(i + searchTerm.length);
lcSpanText = spanText.toLowerCase();
i = -1;
}
}
}
}
}
}
}
}
}
return newText;
return false;
}
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag) {
if (treatAsPhrase) {
searchArray = [searchText];
} else {
searchArray = searchText.split(" ");
}
if (!document.getElementById("spanText") || typeof (document.getElementById("spanText").innerHTML) == "undefined") {
if (warnOnFailure) {
Sorry, for some reason the text of this page is unavailable. Searching will not work.");
}
return false;
}
var spanText = document.getElementById("spanText").innerHTML;
for (var i = 0; i < searchArray.length; i++) {
spanText = doHighlight(spanText, searchArray[i], highlightStartTag, highlightEndTag);
}
document.getElementById("spanText").innerHTML = spanText;
return false;
}
function badphrases(){
highlightSearchTerms('die kill');
}
//window.onload(setTimeout('badphrases();',3000));
//</script>
The code works fine, but when it does a button that does post back with in that div, stops working. The structure is as such:
<div>
<spanText>Text</spanText>
Button
</div>
I thought I found a solution by returning false right after the badphrases() the button still works but then the hightlighting doesn't.
Anyone have ideas?
Thanks.