0

I have put this script on my every html page:

with this css:

strong.searchword {
background-color: #e8d850;
                font-weight:normal;
            }

My highlight.js code is:

function DocSearch() {
    this.highlightWord = function(node,word) {
        // Iterate into this nodes childNodes
        if (node.hasChildNodes) {
            var hi_cn;
            for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
                this.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) {
                pn = node.parentNode;
                // check if we're inside a "nosearchhi" zone
                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!
                    nv = node.nodeValue;
                    ni = tempNodeVal.indexOf(tempWordVal);
                    // Create a load of replacement nodes
                    before = document.createTextNode(nv.substr(0,ni));
                    docWordVal = nv.substr(ni,word.length);
                    after = document.createTextNode(nv.substr(ni+word.length));
                    hiwordtext = document.createTextNode(docWordVal);
                    hiword = document.createElement("strong");
                    hiword.className = "searchword";
                    hiword.appendChild(hiwordtext);
                    pn.insertBefore(before,node);
                    pn.insertBefore(hiword,node);
                    pn.insertBefore(after,node);
                    pn.removeChild(node);
                }
            }
        }
    }   
}

var DOMContentLoaded = false;
function addContentLoadListener (func) {
    if (document.addEventListener) {
        var DOMContentLoadFunction = function () {
            window.DOMContentLoaded = true;
            func();
        };
        document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
    }
    var oldfunc = (window.onload || new Function());
    window.onload = function () {
        if (!window.DOMContentLoaded) {
            oldfunc();
            func();
        }
    };
}

addContentLoadListener( function() {
    var q = window.location.search.substring(1).split('&');
    if(!q.length) 
        return false;
    var docSearch = new DocSearch();
    var bodyEl = document.body;
    for(var i=0; i<q.length; i++){
        var vars = q[i].split('=');
        new DocSearch().highlightWord(bodyEl,decodeURIComponent(vars[1]));
    }
});

/*
window.onload = function() {
    var q = window.location.search.substring(1).split('&');
    if(!q.length) 
        return false;
    var docSearch = new DocSearch();
    var bodyEl = document.body;
    for(var i=0; i<q.length; i++){
        var vars = q[i].split('=');
        new DocSearch().highlightWord(bodyEl,decodeURIComponent(vars[1]));
    }
}

With this url mypage.html?suchwort=rose

I got word rose highlighted. What I am having trouble with is then automatically scrolling to the highlighted word. Is there any way to do this with javascript/jQuery?

  • This might help: http://stackoverflow.com/questions/6677035/jquery-scroll-to-element – luQ Oct 10 '16 at 10:37
  • I have seen that and also demo http://demos.flesler.com/jquery/scrollTo/. But i want this with the help of url, i mean from the external url link. –  Oct 10 '16 at 10:47

0 Answers0