javascrpt:搜索关键字着色

http://www.kryogenix.org/code/browser/searchhi/

http://webdesign.weisshart.de/kryogenix.php

http://www.kryogenix.org/code/browser/searchhi/searchhi.js

/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modified 20081217 to do in-page searching and wrap up in an object */
/* Modified 20081218 to scroll to first hit like 
   http://www.woolyss.free.fr/js/searchhi_Woolyss.js and say when not found */

searchhi = {
  highlightWord: function(node,word) {
    // Iterate into this nodes childNodes
    if (node.hasChildNodes) {
	    for (var hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
		    searchhi.highlightWord(node.childNodes[hi_cn],word);
	    }
    }

    // And do this node itself
    if (node.nodeType == 3) { // text node
	    tempNodeVal = node.nodeValue.toLowerCase();
	    tempWordVal = word.toLowerCase();
	    if (tempNodeVal.indexOf(tempWordVal) != -1) {
		    var pn = node.parentNode;
		    // check if we're inside a "nosearchhi" zone
		    var checkn = pn;
		    while (checkn.nodeType != 9 && 
		    checkn.nodeName.toLowerCase() != 'body') { 
		    // 9 = top of doc
			    if (checkn.className.match(/\bnosearchhi\b/)) { return; }
			    checkn = checkn.parentNode;
		    }
		    if (pn.className != "searchword") {
			    // word has not already been highlighted!
			    var nv = node.nodeValue;
			    var ni = tempNodeVal.indexOf(tempWordVal);
			    // Create a load of replacement nodes
			    var before = document.createTextNode(nv.substr(0,ni));
			    var docWordVal = nv.substr(ni,word.length);
			    var after = document.createTextNode(nv.substr(ni+word.length));
			    var hiwordtext = document.createTextNode(docWordVal);
			    var hiword = document.createElement("span");
			    hiword.className = "searchword";
			    hiword.appendChild(hiwordtext);
			    pn.insertBefore(before,node);
			    pn.insertBefore(hiword,node);
			    pn.insertBefore(after,node);
			    pn.removeChild(node);
			    searchhi.found += 1;
			    if (searchhi.found == 1) pn.scrollIntoView();
		    }
	    }
    }
  },

  googleSearchHighlight: function() {
    var ref = document.referrer;
    if (ref.indexOf('?') == -1) return;
    var qs = ref.substr(ref.indexOf('?')+1);
    var qsa = qs.split('&');
    for (var i=0;i<qsa.length;i++) {
	    var qsip = qsa[i].split('=');
      if (qsip.length == 1) continue;
      if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
		    var wordstring = unescape(qsip[1].replace(/\+/g,' '));
		    searchhi.process(wordstring);
      }
    }
  },
  
  process: function(wordstring) {
    searchhi.found = 0;
    var words = wordstring.split(/\s+/);
    for (w=0;w<words.length;w++) {
	    searchhi.highlightWord(document.getElementsByTagName("body")[0],words[w]);
    }
    if (searchhi.found === 0) {
      searchhi.nohits();
    }
  },
  
  nohits: function() {
  },
  
  init: function() {
    if (!document.createElement || !document.getElementsByTagName) return;
    // hook up forms of type searchhi
    var frms = document.getElementsByTagName("form");
    for (var i=0; i<frms.length; i++) {
      if (frms[i].className.match(/\bsearchhi\b/)) {
        frms[i].onsubmit = function() {
          var inps = this.getElementsByTagName("input");
          for (var j=0; j<inps.length; j++) {
            if (inps[j].type == "text") {
              searchhi.process(inps[j].value);
              return false;
            }
          }
        };
      }
    }
    // highlight search engine referrer results
    searchhi.googleSearchHighlight();
  }
};

(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st =
setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
document.addEventListener("DOMContentLoaded",i,false); } else if(e){     (
function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
i();t=null;}catch(e){st(arguments.callee,0);}})();}else{window.onload=i;}})(searchhi.init);

  searchhi.vbs

  Function DeCodeAnsi(s)
    Dim i, sTmp, sResult, sTmp1
    sResult = ""
    For i=1 To Len(s)
      If Mid(s,i,1)="%" Then
        sTmp = "&H" & Mid(s,i+1,2)
        If isNumeric(sTmp) Then
          If CInt(sTmp)=0 Then
            i = i + 2
          ElseIf CInt(sTmp)>0 And CInt(sTmp)<128 Then
            sResult = sResult & Chr(sTmp)
            i = i + 2
          Else
            If Mid(s,i+3,1)="%" Then
              sTmp1 = "&H" & Mid(s,i+4,2)
              If isNumeric(sTmp1) Then
                sResult = sResult & Chr(CInt(sTmp)*16*16 + CInt(sTmp1))
                i = i + 5
              End If
            Else
              sResult = sResult & Chr(sTmp)
              i = i + 2
            End If
          End If
        Else
          sResult = sResult & Mid(s,i,1)
        End If
      Else
        sResult = sResult & Mid(s,i,1)
      End If
    Next
    DeCodeAnsi = sResult
  End Function

  

posted @ 2015-12-30 15:06  ®Geovin Du Dream Park™  阅读(275)  评论(0)    收藏  举报