javascript 使用javascript可靠地检测IE8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3609282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting IE8 reliably using javascript
提问by sirlark
I have written a web app that requires IE version 8 (or higher presumably). If I run IE8 on a clean windows install on a VM it reports 'MSIE 8.0' as a user agent when queried with navigator.userAgent in javascript. But on a colleagues windows 7 machine his IE reports version 8 in the Help|About window, but the user agent string is 'MSIE 7.0'.
我编写了一个需要 IE 版本 8(或更高版本)的 Web 应用程序。如果我在 VM 上的干净 Windows 安装上运行 IE8,它会在使用 javascript 中的 navigator.userAgent 查询时将“MSIE 8.0”报告为用户代理。但是在同事的 Windows 7 机器上,他的 IE 在帮助|关于窗口中报告版本 8,但用户代理字符串是“MSIE 7.0”。
I figure that somewhere on his machine there is a setting that's telling IE to spoof the previous version, some kind of compatibility setting I presume, but for the life of me I can't find it. I'm not setting up quirksmode or IE7 compatibility mode from my end.
我想在他的机器上的某个地方有一个设置告诉 IE 欺骗以前的版本,我认为是某种兼容性设置,但对于我的生活,我找不到它。我没有从我的角度设置 quirksmode 或 IE7 兼容模式。
回答by Quentin
<meta http-equiv="X-UA-Compatible" content="IE=8">
<script type="text/javascript">
var is_ie8_or_newer = false;
</script>
<!--[if gte IE 8]>
<script type="text/javascript">
is_ie8_or_newer = true;
</script>
<![endif]-->
回答by Woody
The user agent is not a sensible or reliable way of determining the browser version. Why don't you look for the presence of the feature you require by making it IE8 only and use that? That is a much more reliable method.
用户代理不是确定浏览器版本的明智或可靠方式。为什么不通过仅将其设置为 IE8 来寻找所需功能的存在并使用它?这是一种更可靠的方法。
回答by Pointy
The most entertaining trick I've seen — without having any idea of how efficient it is — is to leverage the IE conditional comment feature dynamically. To do that, your code takes a hidden <div>or a <div>in a document fragment, or whatever, and inserts into it some HTML surrounded by a conditional comment coded to check for a specific browser version:
我见过的最有趣的技巧——不知道它的效率有多高——是动态地利用 IE 条件注释功能。为此,您的代码需要在文档片段中使用隐藏<div>或 a<div>或其他内容,然后将一些 HTML 插入其中,并在其中插入一些被条件注释编码以检查特定浏览器版本的 HTML:
var dummy = document.getElementById('dummy');
dummy.innerHTML = '<!' + '--[if IE 8]>x<![endif]-->';
var isIE8 = dummy.innerHTML === 'x';
IE8 can show a little button next to the URL box that switches the browser between IE7 mode and IE8 mode. You can open up the "Developer Tools" and that'll tell you what the current setting is.
IE8 可以在 URL 框旁边显示一个小按钮,用于在 IE7 模式和 IE8 模式之间切换浏览器。您可以打开“开发人员工具”,它会告诉您当前的设置是什么。
回答by Piskvor left the building
Could you use conditional comments?
你能使用条件注释吗?
<script>
var is_ie8 = false;
</script>
<!--[if IE 8]>
<script>
is_ie8 = true;
</script>
<![endif]-->
回答by Rushyo
It should detect such issues. Alternatively, I'm not sure but IE 8 might switch its User-Agent tag in 'Compatibility Mode'.
它应该检测这样的问题。或者,我不确定但 IE 8 可能会在“兼容模式”中切换其用户代理标记。
回答by geowa4
The only way I can figure out how to get my version of IE8 to say that it is IE7 is to enable Compatibility View. See http://blogs.msdn.com/b/ie/archive/2008/08/27/introducing-compatibility-view.aspx
我能弄清楚如何让我的 IE8 版本说它是 IE7 的唯一方法是启用兼容性视图。请参阅http://blogs.msdn.com/b/ie/archive/2008/08/27/introducing-compatibility-view.aspx
回答by sirlark
Turns out his browser was set to display all 'intranet sites' in compatibility mode. Also, yes, compatibility mode changes the user agent string.
原来他的浏览器被设置为在兼容模式下显示所有“内部网站”。此外,是的,兼容模式会更改用户代理字符串。
回答by Justin
Found this sweet function on GIT (in the comments):
function getIEVersion()
{
var v = 3, div = document.createElement('div'), a = div.all || [];
while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]);
return v > 4 ? v : !v;
};

