搜索文本脚本在IE11中不起作用

时间:2014-01-13 10:40:18

标签: javascript

我正在使用以下脚本:http://www.javascripter.net/faq/searchin.htm 但这对InternetExplorer 11来说已不再适用了。

有什么想法来解决这个问题?并使其适用于所有浏览器?

var TRange=null

function findString (str, win) {
    if (parseInt(navigator.appVersion)<4) return;
    var strFound;
    if (navigator.appName=="Netscape") {

        if (!win) win = self;
        win.focus();

        // NAVIGATOR-SPECIFIC CODE

        strFound=win.find(str);
        if (!strFound) {
            strFound=win.find(str,0,1)
            while (win.find(str,0,1)) continue
        }
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {

    // EXPLORER-SPECIFIC CODE

    if (TRange!=null) {
        TRange.collapse(false)
        strFound=TRange.findText(str)
        if (strFound) TRange.select()
    }
    if (TRange==null || strFound==0) {
        TRange=win.document.body.createTextRange()
        strFound=TRange.findText(str)
        if (strFound) TRange.select()
    }
}
 if (!strFound) alert ("Helaas '"+str+"' werd niet gevonden! Probeer iets anders...")
}  

1 个答案:

答案 0 :(得分:0)

如果问题仅出现在IE11中,请考虑以下代码

<head>
<title></title>
<script type="text/javascript" language="javascript" >
    var TRange = null;
    function findString(str) {
        if (parseInt(navigator.appVersion) < 4) return;
        var strFound;
        if (window.find) {
            // CODE FOR BROWSERS THAT SUPPORT window.find
            strFound = self.find(str);
            if (!strFound) {
                strFound = self.find(str, 0, 1);
                while (self.find(str, 0, 1)) continue;
            }
        }
        else if (navigator.appName.indexOf("Microsoft") != -1 || navigator.appName.toString().toUpperCase()== "NETSCAPE") {
            // EXPLORER-SPECIFIC CODE
            if (TRange != null) {
                TRange.collapse(false);
                strFound = TRange.findText(str);
                if (strFound) TRange.select();
            }
            if (TRange == null || strFound == 0) {
                TRange = self.document.body.createTextRange();
                strFound = TRange.findText(str);
                if (strFound) TRange.select();
            }
        }
        else if (navigator.appName == "Opera") {
            alert("Opera browsers not supported, sorry...")
            return;
        }
        if (!strFound) alert("String '" + str + "' not found!")
        return;
    }
</script>
</head>
<body>
 <input type="button" id="btnchk" onclick="findString('abc');" />
  abc
  <span>
  </span>
  pkr
</body>
</html>