Search the page with JavaScript with IE

2

Good morning!

We are creating a super simple HTML page even for a small document that some users will access. In this document we have a form (input text) of search that will have to return through a "highlight" what the user searched for. It is like using CTRL + F even though through the search field, because the user does not want to search with CTRL + F or even sometimes does not know of the command. I searched for some JAVASCRIPT on the Internet, I found some results. Most work in Chrome, but in IE 11 it does not. As the application needs to be run in IE, because not all of them have Chrome in the corporation, it had to be by IE itself.

Example code that worked in Chrome but not in IE:

function doSearch(text) {
    if (window.find && window.getSelection) {
        document.designMode = "on";
        var sel = window.getSelection();
        sel.collapse(document.body, 0);

        while (window.find(text)) {
            document.getElementById("button").blur();
            document.execCommand("HiliteColor", false, "yellow");
            sel.collapseToEnd();
        }
        document.designMode = "off";
    } else if (document.body.createTextRange) {
        var textRange = document.body.createTextRange();
        while (textRange.findText(text)) {
            textRange.execCommand("BackColor", false, "yellow");
            textRange.collapse(false);
        }
    }
}

<input type="text" id="search">
<input type="button" id="button" onmousedown="doSearch(document.getElementById('search').value)" value="Find">

Any idea of any code that works?

    
asked by anonymous 10.09.2015 / 15:33

0 answers