Firefox36和IE11浏览器检测问题

时间:2015-05-05 14:57:12

标签: javascript google-chrome internet-explorer browser-detection

我已经创建了一个脚本来检测浏览器详细信息(浏览器版本,用户代理等),使用chrome工作得很好,但问题是IE11和FF36。从一开始,我就让chrome和FF正常工作,但IE浏览器名称显示“Netscape”。经过一些研究后,我发现“MSIE”令牌已被删除并替换为“rv”。所以我在我的JS中将IE的令牌更改为“rv”并运行脚本,但是当我在FF上测试它时,它然后将浏览器名称显示为“Microsoft Internet Explorer”。因此,如果有人能够让我深入了解为什么或者我在代码中遗漏了哪些内容会很棒。

    //OS version
        var osversion = navigator.platform;

        var objappVersion = navigator.appVersion;
        var objAgent = navigator.userAgent;
        var objbrowserName = navigator.appName;
        var objfullVersion = '' + parseFloat(navigator.appVersion);
        var objBrMajorVersion = parseInt(navigator.appVersion, 10);
        var objOffsetName, objOffsetVersion, ix;

        // In Chrome 
        if ((objOffsetVersion = objAgent.indexOf("Chrome")) != -1) {
            objbrowserName = "Chrome";
            objfullVersion = objAgent.substring(objOffsetVersion + 7);
        }
        // In Microsoft internet explorer
        else if ((objOffsetVersion = objAgent.indexOf("MSIE")) != -1) {
            objbrowserName = "Microsoft Internet Explorer";
            objfullVersion = objAgent.substring(objOffsetVersion + 5);
        }

        // In Firefox
        else if ((objOffsetVersion = objAgent.indexOf("Firefox")) != -1) {
            objbrowserName = "Firefox";
        }
        // In Safari 
        else if ((objOffsetVersion = objAgent.indexOf("Safari")) != -1) {
            objbrowserName = "Safari";
            objfullVersion = objAgent.substring(objOffsetVersion + 7);
            if ((objOffsetVersion = objAgent.indexOf("Version")) != -1)
                objfullVersion = objAgent.substring(objOffsetVersion + 8);
        }
        // For other browser "name/version" is at the end of userAgent 
        else if ((objOffsetName = objAgent.lastIndexOf(' ') + 1) <
                    (objOffsetVersion = objAgent.lastIndexOf('/'))) {
            objbrowserName = objAgent.substring(objOffsetName, objOffsetVersion);
            objfullVersion = objAgent.substring(objOffsetVersion + 1);
            if (objbrowserName.toLowerCase() == objbrowserName.toUpperCase()) {
                objbrowserName = navigator.appName;
            }
        }
        // trimming the fullVersion string at semicolon/space if present
        if ((ix = objfullVersion.indexOf(";")) != -1)
            objfullVersion = objfullVersion.substring(0, ix);
        if ((ix = objfullVersion.indexOf(" ")) != -1)
            objfullVersion = objfullVersion.substring(0, ix);

        objBrMajorVersion = parseInt('' + objfullVersion, 10);
        if (isNaN(objBrMajorVersion)) {
            objfullVersion = '' + parseFloat(navigator.appVersion);
            objBrMajorVersion = parseInt(navigator.appVersion, 10);
        }

        window.onload = function () {
            document.getElementById("browser").innerHTML = objbrowserName;
            document.getElementById("fullV").innerHTML = objfullVersion;
            document.getElementById("majorV").innerHTML = objbrowserName + " " + objBrMajorVersion;
            document.getElementById("appName").innerHTML = navigator.appName;
            document.getElementById("userAgent").innerHTML = navigator.userAgent;
}

0 个答案:

没有答案