gpt4 book ai didi

javascript - MSIE版本不同

转载 作者:行者123 更新时间:2023-11-30 13:30:34 26 4
gpt4 key购买 nike

我有一些获取浏览器名称和浏览器版本号的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>

是否有任何原因导致特定站点显示错误的浏览器版本?

最佳答案

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具有兼容 User Agent 的兼容模式(UA) 字符串。因此,您可以使用 IE9,但在 兼容模式 下查看网站,这可以将 UA 更改为 IE8,这可能就是您所看到的。

如果你不介意一个完整库的额外开销,jQuery 有可靠的 browser detection但是内置,我相信如果 IE 欺骗 UA,它仍然会遇到同样的问题。 编辑 实际上,似乎确实有一个解决方法 - 请参阅 Detect IE8 Compatibility Mode ,但同样可能只是用于浏览器检测的大量开销。

但总的来说(正如其他人所提到的)UA 嗅探是不可靠的,因为它很容易被欺骗。

关于javascript - MSIE版本不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6918106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com