home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / system / diskche / diskcheckup.exe / HELP / highlight.js < prev    next >
Text File  |  2007-10-30  |  8KB  |  252 lines

  1. // ----------------------------------------------------------------------------
  2. // Zoom Search Engine 4.3 (27/6/2006)
  3. // Highlight & auto-scroll script
  4. //
  5. // email: zoom@wrensoft.com
  6. // www: http://www.wrensoft.com
  7. //
  8. // Copyright (C) Wrensoft 2006
  9. // ----------------------------------------------------------------------------
  10. // Use this script to allow your search matches to highlight and scroll to
  11. // the matched word on the actual web page where it was found.
  12. //
  13. // You will need to link to this JS file from each page of your site
  14. // which requires the "highlight/jump to matched word" feature.
  15. //
  16. // For example, you could paste the following HTML in your site's header or 
  17. // footer:
  18. //
  19. //   <style>.highlight { background: #FFFF40; }</style>
  20. //   <script type="text/javascript" src="highlight.js"></script>
  21. //
  22. // Note: You will need to specify the correct path to "highlight.js" depending
  23. // on where the file is located.
  24. //
  25. // You will then need to modify the BODY tag on your page to include an "onLoad" 
  26. // attribute, such as:
  27. //
  28. //   <body onload="highlight();">
  29. //
  30. // If for some reason you can not modify the body tag of your page, an alternative
  31. // would be to put the following line after the </body> tag of your page:
  32. //
  33. //   <script type="text/javascript">highlight();</script>
  34. //
  35. // For more information, consult the Users Guide and our support website at:
  36. // http://www.wrensoft.com/zoom/support
  37. //
  38. //
  39. // This script is licensed for use with the Zoom Search Engine. Original
  40. // development by Brett Alcock, VBasys Limited. To licence other applications 
  41. // email highlight@vbasys.com with the subject "license".
  42.  
  43. var CatchJSErrors = true;
  44.  
  45. function catcherror() { return true; }
  46. if (CatchJSErrors)
  47. {
  48.     window.onerror = catcherror;
  49. }
  50.  
  51. function QueryString(key)
  52. {
  53.     var value = null;
  54.     for (var i=0;i<QueryString.keys.length;i++)
  55.     {
  56.         if (QueryString.keys[i]==key)
  57.         {
  58.             value = QueryString.values[i];
  59.             break;
  60.         }
  61.     }
  62.     return value;
  63. }
  64.  
  65. function QueryString_Parse()
  66. {
  67.     var query = window.location.search.substring(1);
  68.     var pairs = query.split("&");
  69.  
  70.     for (var i=0;i<pairs.length;i++)
  71.     {
  72.         var pos = pairs[i].indexOf('=');
  73.         if (pos >= 0)
  74.         {
  75.             var argname = pairs[i].substring(0,pos);
  76.             var value = pairs[i].substring(pos+1);
  77.             QueryString.keys[QueryString.keys.length] = argname;
  78.             QueryString.values[QueryString.values.length] = value;
  79.         }
  80.     }
  81. }
  82.  
  83. QueryString.keys = new Array();
  84. QueryString.values = new Array();
  85.  
  86. QueryString_Parse();
  87.  
  88. function getElement(id)
  89. {
  90.     if (document.getElementById)
  91.         return(document.getElementById(id));
  92.     else if (document.all)
  93.         return(document.all[id]);
  94. }
  95.  
  96. function findPosY(obj)
  97. {
  98.     var curtop = 0;
  99.     if (obj.offsetParent)
  100.     {
  101.         while (obj.offsetParent)
  102.         {
  103.             curtop += obj.offsetTop
  104.             obj = obj.offsetParent;
  105.         }
  106.     }
  107.     else if (obj.y)
  108.         curtop += obj.y;
  109.     return curtop;
  110. }
  111.  
  112.  
  113. // regular expression version
  114. function SearchHiLite(text)
  115. {
  116.     var SearchAsSubstring = 0;
  117.     var hl;
  118.  
  119.     hl = QueryString("zoom_highlight");
  120.     if (hl == "" || hl == null)
  121.     {
  122.         hl = QueryString("zoom_highlightsub");
  123.         if (hl == "" || hl == null)
  124.             return false;
  125.         else
  126.             SearchAsSubstring = 1;
  127.     }
  128.     if ((document.charset && document.charset == "utf-8") ||
  129.         (document.characterSet && document.characterSet == "UTF-8"))
  130.         hl = decodeURIComponent(hl);
  131.     else
  132.         hl = unescape(hl);
  133.     hl = hl.toLowerCase();
  134.                 
  135.     // create array of terms        
  136.     //var term = hl.split("+"); 
  137.     var re = /\"(.*?)\"|[^\\+\"]+/g;
  138.     var term = hl.match(re);    
  139.    
  140.     // convert terms in regexp patterns
  141.     for (var i=0;i<term.length;i++) // take each term in turn
  142.     {       
  143.         if(term[i] != "")
  144.         {                   
  145.             if (term[i].indexOf("\"") != -1)
  146.             {
  147.                 // contains double quotes               
  148.                 term[i]=term[i].replace(/\"/g,"");
  149.                 term[i]=term[i].replace(/\+/g," "); 
  150.             }
  151.             else
  152.             {
  153.                 term[i]=term[i].replace(/\+/g,"");  
  154.             }                           
  155.  
  156.             if (term[i].indexOf("*") != -1 || term[i].indexOf("?") != -1)
  157.             {
  158.                 // convert wildcard pattern to regexp
  159.                 term[i] = term[i].replace(/\\/g, " ");
  160.                 term[i] = term[i].replace(/\^/g, " ");
  161.  
  162.                 //term[i] = term[i].replace(/\+/g, " "); // split on this so no point in looking
  163.  
  164.                 term[i] = term[i].replace(/\#/g, " ");
  165.                 term[i] = term[i].replace(/\$/g, " ");
  166.                 term[i] = term[i].replace(/\./g, " ");
  167.                 
  168.                 // check if search term only contains only wildcards
  169.                 // if so, we will not attempt to highlight this term
  170.                 var wildcards = /\w/;
  171.                 if (wildcards.test(term[i]))
  172.                 {
  173.                     term[i] = term[i].replace(/\*/g, "[^\\s]*");
  174.                     term[i] = term[i].replace(/\?/g, "[^\\s]"); // insist upon one non whitespace
  175.                 }                
  176.                 else                
  177.                     term[i] = "";                
  178.             }
  179.             
  180.             if (term[i] != "")
  181.             {
  182.                 if (SearchAsSubstring == 0)
  183.                 {                    
  184.                     term[i] = "(>[\\s]*|>[^<]+[\\b\\W])("+term[i]+")(<|[\\b\\W][^>]*<)";
  185.                 }
  186.                 else
  187.                 {
  188.                     // if term leads with wildcard then allow it to match preceeding text in word
  189.                     var strWB="";
  190.                     if(term[i].substr(0,7)=="[^\\s]*") strWB="\\b";
  191.                     term[i] = "(>|>[^<]+)"+strWB+"("+term[i]+")([^>]*<)";
  192.                 }
  193.             }            
  194.         }
  195.     }
  196.  
  197.     text=text.replace(/&/ig, '&');
  198.     text=text.replace(/ /ig, '');
  199.  
  200.     for (var i=0;i<term.length;i++) // take each term in turn
  201.     {
  202.         if(term[i] != "")
  203.         {                        
  204.             // we need a loop for the main search to catch all between ><
  205.             // and we add  before each found to ignore those done etc
  206.             // todo: develop reliable single pass regexp and dispose of loop
  207.             var l = 0;
  208.             re = new RegExp(term[i], "gi");
  209.             var count = 0; // just incase
  210.             text = ">" + text + "<"; // temporary tag marks
  211.             do 
  212.             {
  213.                 l=text.length;
  214.                 text=text.replace(re, '$1<span style="background:#FFFF40;" class="highlight" id="highlight" name="highlight">$2</span id="highlight">$3');
  215.                 count++;
  216.             }
  217.             //while(re.lastIndex>0 && count<100); lastIndex not set properly under netscape
  218.             while(l!=text.length && count<100);
  219.             text = text.substring(1, text.length-1); // remove temporary tags
  220.         }
  221.     }        
  222.     text = text.replace(eval("//g"), '');        
  223.     text = text.replace(eval("//g"), ' ');    
  224.        
  225.     return(text);
  226. }
  227.  
  228. function jumpHL()
  229. {
  230.     var d=getElement("highlight");
  231.     if(d)
  232.     {
  233.         var y=findPosY(d);
  234.         // if element near top of page
  235.         if(y < 100)
  236.             window.scrollTo(0,0); // go to top of page
  237.         else
  238.             window.scrollTo(0,y-50); // show space of 50 above
  239.     }
  240. }
  241.  
  242. function highlight()
  243. {    
  244.     var x = document.body;
  245.     if (x)
  246.     {
  247.         var strHTML=SearchHiLite(x.innerHTML);
  248.         if (strHTML!=false) x.innerHTML = strHTML;
  249.         jumpHL();
  250.     }
  251. }
  252.