Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeUsing DNN Platf...Using DNN Platf...Using Modules a...Using Modules a...Javacsript and PostbackJavacsript and Postback
Previous
 
Next
New Post
6/30/2014 7:58 PM
 

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.


Thuan
 
New Post
7/1/2014 10:28 AM
 
javascript and asp.net postbacks are somewhat at odds with each other under certain circumstances.
This is especially the case if there are any async / partial update panels on the page
- something that is more and more common these days.

Basically - if your script is attached to a html block that is functioning inside an update panel
- you need to tell the webpage to rebind the javascript after each postback.

Without seeing the exact setup of your site - you should try having a look at
using the pageLoad system event in combination with the document ready function.

function pageLoad(sender, args)
{
  $(document).ready(function(){   

   // put all your javascript functions here 

  });
}

 
New Post
7/2/2014 3:02 AM
 

Thank you Wes for the reply. I couldn't use your suggestion but I did eventually find something that worked. Here's the code for others that might need it:

<style>.highlight { background-color: yellow; }</style>

<script language="javascript" type="text/javascript">

jQuery.fn.highlight = function(pat) {
 function innerHighlight(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'highlight';
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.length && pat && pat.length ? this.each(function() {
  innerHighlight(this, pat.toUpperCase());
 }) : this;
};

$(document).ready(function(){  
 $("td").highlight("kill");
});

</script>

Happy Coding to all!


Thuan
 
Previous
 
Next
HomeHomeUsing DNN Platf...Using DNN Platf...Using Modules a...Using Modules a...Javacsript and PostbackJavacsript and Postback


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out