开发者

MSIE version is different

I have some javascript that gets the browser name and version number of the browser. Everything works fine; I get the correct name and versions correctly, unless it's a specific site. On that specific site, instead of my IE9 showing version 9, version 8 is shown. If I test in other browsers, again, the name and versions are correct. The problem is just within IE. It's not just IE9 either; IE8 on that specific site shows version 7. I've also just made a separate HTML page with just the javascript and I get the correct version numbers. The code is as follows:

<html>
    <head>

        <script type="text/javascript">
    var BrowserDetect = {
            init: function () {
                this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
                this.version = this.searchVersion(navigator.userAgent)
                    || this.searchVersion(navigator.appVersion)
                    || "an unknown version";
                this.OS = this.searchString(this.dataOS) || "an unknown OS";
            },
            searchString: function (data) {
                for (var i=0;i<data.length;i++) {
                    var dataString = data[i].string;
                    var dataProp = data[i].prop;
                    this.versionSearchString = data[i].versionSearch || data[i].identity;
                    if (dataString) {
                        if (dataString.indexOf(data[i].subString) != -1)
                            return data[i].identity;
                    }
                    else if (dataProp)
                        return data[i].identity;
                }
            },
            searchVersion: function (dataString) {
                alert("this.versionSearchString: " + this.versionSearchString);
                alert("useragent: " + dataString);
                var index = dataString.indexOf(this.versionSearchString);
                if (index == -1) return;
                return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
            },
            dataBrowser: [
                {
                    string: navigator.userAgent,
                    subString: "Chrome",
                    identity: "Chrome"
                },
                {   string: navigator.userAgent,
                    subString: "OmniWeb",
                    versionSearch: "OmniWeb/",
                    identity: "OmniWeb"
                },
                {
                开发者_Python百科    string: navigator.vendor,
                    subString: "Apple",
                    identity: "Safari",
                    versionSearch: "Version"
                },
                {
                    prop: window.opera,
                    identity: "Opera"
                },
                {
                    string: navigator.vendor,
                    subString: "iCab",
                    identity: "iCab"
                },
                {
                    string: navigator.vendor,
                    subString: "KDE",
                    identity: "Konqueror"
                },
                {
                    string: navigator.userAgent,
                    subString: "Firefox",
                    identity: "Firefox"
                },
                {
                    string: navigator.vendor,
                    subString: "Camino",
                    identity: "Camino"
                },
                {       // for newer Netscapes (6+)
                    string: navigator.userAgent,
                    subString: "Netscape",
                    identity: "Netscape"
                },
                {
                    string: navigator.userAgent,
                    subString: "MSIE",
                    identity: "Explorer",
                    versionSearch: "MSIE"
                },
                {
                    string: navigator.userAgent,
                    subString: "Gecko",
                    identity: "Mozilla",
                    versionSearch: "rv"
                },
                {       // for older Netscapes (4-)
                    string: navigator.userAgent,
                    subString: "Mozilla",
                    identity: "Netscape",
                    versionSearch: "Mozilla"
                }
            ],
            dataOS : [
                {
                    string: navigator.platform,
                    subString: "Win",
                    identity: "Windows"
                },
                {
                    string: navigator.platform,
                    subString: "Mac",
                    identity: "Mac"
                },
                {
                       string: navigator.userAgent,
                       subString: "iPhone",
                       identity: "iPhone/iPod"
                },
                {
                    string: navigator.platform,
                    subString: "Linux",
                    identity: "Linux"
                }
            ]

        };
        BrowserDetect.init();
    </script>
        <script>
        function browserCheck() {
            var invalid = false;
            var browser = BrowserDetect.browser;
            var version = BrowserDetect.version;

            alert("Browser: " + browser + ", Version: " + version);



    if (browser == 'Firefox'){
            if (parseFloat(version) < parseFloat('3.0')){
                invalid = true;
            }
        } else if (browser == 'Explorer'){
            if (parseFloat(version) < parseFloat('8.0')){
                invalid = true;
            }
        } else {
            invalid = true;
        }

        if (invalid){
            //some action
        }
    }
    </script>
</head>
<body onload="browserCheck()">

</body>
</html>

Is there any reason why a specific site would show the wrong browser version?


IE9 https://learn.microsoft.com/en-us/archive/blogs/ie/introducing-ie9s-user-agent-string and IE8 https://learn.microsoft.com/en-us/archive/blogs/ie/the-internet-explorer-8-user-agent-string-updated-edition have compatibility modes that fiddle with the User Agent (UA) string. So you can be using IE9 but viewing the website in Compatilbility mode which can change the UA to IE8, which is probably what you are seeing.

If you don't mind the added overhead of a complete library, jQuery has solid browser detection built in however, I believe it would still suffer the same problem if IE is spoofing the UA. Edit Actually, there does seem to be a workaround - see Detect IE8 Compatibility Mode, but again it's probably a lot of overhead just for browser detection.

Overall though (and as others have mentioned) UA sniffing is unreliable because it can be spoofed so easily.


What Jason said. You seem to be under the impression that browser detection is reliable; in fact, you're just looking at whatever string the vendor decided to use. Microsoft are notorious for lying in their User-Agent strings; notice how they've written "Mozilla" in them for the past 20 years.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜