Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IE6 and small highlite script

    I needed something really simple that would highlite the results of a fulltext search in my asp.net application, and decided on javascript. I managed to get it working wuite well (despite the fact that im using a regular expression to keep out of html tags, which was tricky and the regexp sometimes misses a keyword, but it doesnt mess up my page so its all good)- well actually, it does mess up my page, but only, strictly on IE6... i havent tested 7 though... firefox (v2.0.0.4) wont even give me a warning or an error in the javascript error console, and I've used pretty much very simple javascript.

    Code:
    function HighliteKeywords(strSearch)
      {
        var keywords = new Array(); 
        keywords = strSearch.split(' '); 
        var rng; 
        if (keywords.length >= 1) 
        { 
          for(k=0;k<keywords.length;k++) 
          { 
            var regEx = new RegExp('((<[^<]*>)+[^<]*)('+keywords[k]+')', "gi"); 
            var form = document.getElementById('aspnetForm'); 
            rng = form.innerHTML;
            form.innerHTML = rng.replace(regEx, "$1<span style='color: #000000;font-weight:bold;background-color: #dddd00;'>$3</span>");
          }
        } 
       }
    I'm passing the strSearch as a uery parameter and use Page.MasterPage.FindControl("body") so as to find the body and add the onload attribute to it, cuz IE didnt like me putting up the function call on the page PreRender by assigning it to a literal, either. So i got it working on IE, then changed a few things in my regular expression to cover the html tags and tested on firefox, and it works nickel... now i just tried it in IE6 again... BAM, I try to load the page, and it just seems like its stuck in an infinite loop and wont finish loading, neither can i do anything but click on the x in the top and try again.

    sadly i dont have a link to share for this cuz im developping locally and only transferring when I know nothing will bug up dramatically, and thats a very annoying bug. Anyone's got an idea?

    *edit* Heh... well I recompiled and it seems like its not doing it anymore... I didnt change anything either to the script or the basic layout of the page, all I,ve been working on is some data access so I'm really confused as to what couldve caused this. Could it be that some AJAX component conflict with the javascript or vice-versa?
    Last edited by Krokador; 07-04-2007 at 05:17 PM.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var zxcWordAry=[];
    
    function zxcWords(zxcN){
     var zxcnu=zxcN.childNodes.length;
     for(var zxc0=0;zxc0<zxcnu;zxc0++) {
      var zxcTxt=zxcN.firstChild.data;
      var zxc=zxcN.removeChild(zxcN.firstChild);
      if(zxc.nodeType==3){
       zxcAry=zxcTxt.split(' ');
       for(var zxc1=0;zxc1<zxcAry.length; zxc1++) {
        var zxcT=document.createElement('SPAN');
        zxcWordAry.push(zxcT);
        var zxcTTxt=document.createTextNode(zxcAry[zxc1]+' ');
        zxcT.appendChild(zxcTTxt);
        zxcN.appendChild(zxcT);
       }
      }
      else {
       zxcWords(zxc);
       zxcN.appendChild(zxc);
      }
     }
    }
    
    function zxcFindTxt(zxctxt){
     for (var zxc0=0;zxc0<zxcWordAry.length;zxc0++){
      zxcES(zxcWordAry[zxc0],{backgroundColor:(zxcWordAry[zxc0].firstChild.data.match(zxctxt))?'#ff0000':'#ffffff'});
    
     }
    
    
    }
    
    function zxcES(zxcele,zxcstyle,zxcp,zxctxt){
     if (typeof(zxcele)=='string'){ zxcele=document.createElement(zxcele); }
     for (key in zxcstyle){ zxcele.style[key]=zxcstyle[key]; }
     if (zxcp){ zxcp.appendChild(zxcele); }
     if (zxctxt){ zxcele.appendChild(document.createTextNode(zxctxt)); }
     return zxcele;
    }
    //-->
    </script></head>
    
    <body onload="zxcWords(document.getElementById('test1'))">
    <p id="test1" >
    Some words
    </p>
    <input type="Button" value="Find 'Some'" onclick="zxcFindTxt('Some');" >
    <input type="Button" value="Find 'words'" onclick="zxcFindTxt('words');" >
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •