javascript 区分IE7浏览器和IE7兼容模式下的浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10213639/
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
Differentiate IE7 browser and browser in IE7 compatibility mode
提问by vault-boy
Can I differentiate if client's browser is IE7 or e.g. IE9 in IE7 compatibility mode? I'm trying to figure out if I can do a JS check on my site which would recognize two different things and do different stuff depending on the result
我可以在 IE7 兼容模式下区分客户端的浏览器是 IE7 还是 IE9 吗?我想弄清楚我是否可以在我的网站上做一个 JS 检查,它会识别两种不同的东西并根据结果做不同的事情
- that browser is IE7
- that browser is in IE7 compatibility mode
- 那个浏览器是IE7
- 该浏览器处于 IE7 兼容模式
I have the first condition working correctly as it's pretty much said everywhere how to do it. Not sure about the second one and/or combination of both.
我有第一个条件正常工作,因为几乎到处都在说如何做到这一点。不确定第二个和/或两者的组合。
采纳答案by tuomassalo
For at least IE8 and IE9, you can check whether navigator.userAgent
has the substring Trident
in it. An IE8+ alwayshas a Trident
in its user-agent, where an IE7 doesn't. See this answerand the MSDN link in it.
至少对于 IE8 和 IE9,您可以检查其中是否navigator.userAgent
包含子字符串Trident
。IE8+的用户代理中总是有一个Trident
,而 IE7 没有。请参阅此答案和其中的 MSDN 链接。
IE10 seems trickier: it is reported in the comments below that Trident
is not alwayspresent with IE7 emulation mode. Probably the OS string (eg. Windows NT 6.2
) will still reveal IE10, if IE10 will not be available on any platform where IE7 is available.
IE10 似乎更棘手:在下面的评论中报告说,IE7 仿真模式Trident
并不总是存在。如果 IE10 在 IE7 可用的任何平台上不可用,操作系统字符串(例如Windows NT 6.2
)可能仍会显示 IE10。
Please also note that the HTTP User-Agent
header might not always match navigator.userAgent
. This is the case at least with IE9 that has compatiblity mode on (sends an IE7 User-Agent
header) but detects something like IE=Edge
in the response (navigator.userAgent
turns back to IE9).
另请注意,HTTPUser-Agent
标头可能并不总是匹配navigator.userAgent
。至少在启用了兼容模式(发送 IE7User-Agent
标头)但IE=Edge
在响应中检测到类似内容的IE9 是这种情况(navigator.userAgent
返回到 IE9)。
回答by Sampson
I don't believe there is a way to detect if the user's browser is in compat mode. Their user agent string will be determined by their browser mode, and their document mode will be determined by either the presence of an x-ua-compatible
meta tag (or header), or possibly by the doctype used.
我不相信有一种方法可以检测用户的浏览器是否处于兼容模式。他们的用户代理字符串将由他们的浏览器模式决定,他们的文档模式将由x-ua-compatible
元标记(或标题)的存在或可能由使用的文档类型决定。
Compatibility Mode was meant to protect the modern-browser-user from pages that relied on old and outdated features or hacks. It's not really something you would want to test against. Instead, write standards-compliant code which will be understood by the browser in either compat mode, or non-compat mode.
兼容模式旨在保护现代浏览器用户免受依赖于旧的和过时的功能或黑客的页面的影响。这不是你真正想要测试的东西。相反,编写符合标准的代码,浏览器可以在兼容模式或非兼容模式下理解这些代码。
Here are the various results of differing Browser Modes and Document Modes:
以下是不同浏览器模式和文档模式的各种结果:
Browser Mode: IE10 Compat View / Document Mode: IE7 standards
浏览器模式:IE10 兼容视图/文档模式:IE7 标准
navigator.userAgent
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0;
.NET4.0E; .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729;
.NET CLR 2.0.50727; .NET CLR 3.0.30729; BRI/2)"
document.documentMode
7
Browser Mode: IE7 / Document Mode: IE7 standards
浏览器模式:IE7 / 文档模式:IE7 标准
navigator.userAgent
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; .NET4.0E;
.NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727;
.NET CLR 3.0.30729; BRI/2)"
document.documentMode
7
As you can see, by these two methods there is no way to tell if the user is in compat view or not.
如您所见,通过这两种方法无法判断用户是否处于兼容视图中。
Overriding Compat View List
覆盖兼容视图列表
If your site appears on the compatibility view list, you can override their suggested rendering options by providng your own x-ua-compatible
header:
如果您的站点出现在兼容性视图列表中,您可以通过提供您自己的x-ua-compatible
标题来覆盖他们建议的呈现选项:
<meta http-equiv="x-ua-compatible" content="IE=9" />
This forces your browser into IE9 Standards Mode (no evaluation of the doctype). You could use IE=edge
to force it into the latest mode possible (on Internet Explorer 10, this would be IE 10 Standards), but this is not encouraged. Instead, set it to the latest mode you've tested with.
这会强制您的浏览器进入 IE9 标准模式(不评估文档类型)。您可以使用IE=edge
它强制进入可能的最新模式(在 Internet Explorer 10 上,这将是 IE 10 标准),但不鼓励这样做。相反,将其设置为您测试过的最新模式。
If the x-ua-compatible
header is set to IE10, but the user visits your page on an earlier browser, the nearest rendering engine will be used. For instance, if the user visits your page with IE9, and your meta tag instructs the browser to use IE10, the browser will fallback to IE9 Standards mode.
如果x-ua-compatible
标题设置为 IE10,但用户在较早的浏览器上访问您的页面,则将使用最近的渲染引擎。例如,如果用户使用 IE9 访问您的页面,并且您的元标记指示浏览器使用 IE10,则浏览器将回退到 IE9 标准模式。
Note that IE=9
causes the browser to go into IE9 Standards Mode. It doesn't necessarily cause the browser to behave as though it were IE9. If you want the browser to behave as though it were IE9, you would want to use the EmulateIE9
content:
请注意,这IE=9
会导致浏览器进入 IE9 标准模式。它不一定会导致浏览器表现得像 IE9 一样。如果您希望浏览器像 IE9 一样运行,您需要使用以下EmulateIE9
内容:
<meta http-equiv="x-ua-compatible" content="IE=EmulateIE9" />
This causes the browser to fallback on the DOCTYPE, if it's present, to determine whether the document mode will be Standards, or Quirks.
这会导致浏览器回退到 DOCTYPE(如果存在),以确定文档模式是标准还是 Quirks。
For further information, see Defining Document Compatibility.
有关详细信息,请参阅定义文档兼容性。
回答by Matt Robertson
For anyone reaching this thread on Google, here is another option:
对于在 Google 上访问此线程的任何人,这是另一种选择:
Using the script located at http://www.quirksmode.org/js/detect.html....
使用位于http://www.quirksmode.org/js/detect.html的脚本......
if (BrowserDetect.browser == 'Explorer')
{
if (document.documentMode == 7 && BrowserDetect.version > 7)
{
// user is running IE in compatability mode
}
}
回答by felvhage
This works for me:
这对我有用:
/**
* Actual IE8-Browser running in IE8-Compat-Mode or IE7 Mode?
*/
MyUtils.isIE8CompatMode= function(){
if($.browser.msie && navigator.userAgent.indexOf('MSIE 7.0')>=0){
return true;
}
return false;
}
An actual IE8 browser has 3 Document-Modes (IE7, IE8 & Quirks) and 3-Browser-Modes (IE7, IE8 and IE8-Compatibility). We can enforce the Document-Mode to be IE8 with:
一个实际的 IE8 浏览器有 3 种文档模式(IE7、IE8 和 Quirks)和 3 种浏览器模式(IE7、IE8 和 IE8 兼容性)。我们可以通过以下方式强制文档模式为 IE8:
<meta http-equiv="X-UA-Compatible" content="IE=8,chrome=1"/>
But, we cannot enforce the Browser-Mode, which is determined by Browser-configuration. For example: some of our customers have the "render all pages in compatibility-mode"-checkbox checked in their Compatibility-View-Settings-Dialog. If that is the case, we can do nothing about it.
但是,我们不能强制执行由浏览器配置决定的浏览器模式。例如:我们的一些客户在他们的兼容性视图设置对话框中选中了“以兼容模式呈现所有页面”复选框。如果是这样,我们也无能为力。
See the very enlightening diagram on this site:
请参阅此站点上非常有启发性的图表:
-> How ie8 determines compatibility-mode
-> ie8 如何确定兼容模式
What do we use the above function for?Since our Page shows some glitches when in compatibility-mode we need to tell the user, why it is looking bad and what he can do about it. So we use the above function to tell if we are in trouble and then render a small warning to the user.
我们用上面的函数做什么?由于我们的页面在兼容模式下会显示一些小故障,我们需要告诉用户为什么它看起来很糟糕以及他可以做些什么。所以我们使用上面的函数来判断我们是否遇到了麻烦,然后向用户呈现一个小警告。
In ie9 and ie10 compatiblity-modes our app looks fine, so we dont need it there. This response may be considered an answer to this question: detect ie8 compatibility modewhich was marked as a duplicate of this one. Perhaps it helps someone.
在 ie9 和 ie10 兼容模式下,我们的应用程序看起来不错,所以我们不需要它。此回复可被视为对以下问题的回答:检测被标记为与此模式重复的ie8 兼容模式。也许它可以帮助某人。
回答by seangates
Here is a fairly bullet-proof check that I did for a large-scale auction site based in San Jose.
这是我为位于圣何塞的一家大型拍卖网站所做的相当可靠的检查。
var userAgentString = navigator.userAgent;
if(/MSIE 7\.0/gi.test(userAgentString) && /Trident/gi.test(userAgentString)){
// compatibility mode
// .... code goes here ....
}