我有一些javascript可以获取浏览器的浏览器名称和版本号。一切正常;我正确地得到了正确的名称和版本,除非它是一个特定的网站。在该特定站点上,而不是我的IE9显示版本9,显示版本8。如果我再次在其他浏览器中测试,则名称和版本是正确的。问题只在IE中。它不仅仅是IE9;该特定网站上的IE8显示版本7.我还刚刚使用javascript创建了一个单独的HTML页面,我得到了正确的版本号。代码如下:
<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"
},
{
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>
为什么特定网站会显示错误的浏览器版本?
答案 0 :(得分:2)
IE9 http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx和IE8 http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx具有兼容User Agent(UA)字符串的兼容模式。因此,您可以使用IE9,但可以在 Compatilbility模式中查看网站,这可以将UA更改为IE8,这可能就是您所看到的。
如果你不介意完整库的额外开销,jQuery有内置的browser detection,我相信如果IE欺骗UA,它仍会遇到同样的问题。 修改实际上,确实有一种解决方法 - 请参阅Detect IE8 Compatibility Mode,但同样可能需要大量的开销 进行浏览器检测。
总的来说(和其他人一样)UA嗅探是不可靠的,因为它可以很容易被欺骗。
答案 1 :(得分:1)
杰森说。您似乎认为浏览器检测是可靠的;实际上,您只是在查看供应商决定使用的任何字符串。微软因其用户代理字符串而臭名昭着;注意他们在过去的20年里是如何写出“Mozilla”的。