MSIE版本不同
我有一些 javascript 可以获取浏览器名称和浏览器版本号。一切正常;我正确地获得了正确的名称和版本,除非它是特定的站点。在该特定站点上,我的 IE9 显示的是版本 8,而不是版本 9。如果我在其他浏览器中再次测试,名称和版本是正确的。问题出在 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>
有什么原因导致特定网站会显示错误的浏览器版本吗?
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"
},
{
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IE9 https://learn.microsoft .com/en-us/archive/blogs/ie/introducing-ie9s-user-agent-string 和 IE8 https: //learn.microsoft.com/en-us/archive/blogs/ie/the-internet-explorer-8-user-agent-string-updated-edition 具有与 用户代理 (UA) 字符串。因此,您可以使用 IE9,但在兼容模式下查看网站,这可以将 UA 更改为 IE8,这可能就是您所看到的。
如果您不介意完整库的额外开销,jQuery 具有可靠的浏览器检测不过,我相信如果 IE 欺骗 UA,它仍然会遇到同样的问题。 编辑实际上,似乎确实有一个解决方法 - 请参阅检测 IE8 兼容模式< /a>,但同样,浏览器检测可能会产生大量开销。
总体而言(正如其他人所提到的)UA 嗅探并不可靠,因为它很容易被欺骗。
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.
杰森说的话。您似乎认为浏览器检测是可靠的;事实上,您只需查看供应商决定使用的任何字符串。微软因在用户代理字符串中撒谎而臭名昭著。请注意过去 20 年来他们如何在其中写入“Mozilla”。
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.