IE11 通过 javascript 检测兼容性视图是否开启
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27912296/
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
IE11 detect whether compatibility view is ON via javascript
提问by slee
does anyone know how to check if IE 11 compatibility mode is ON when I'm on a website through javascript?
有谁知道如何在我通过 javascript 访问网站时检查 IE 11 兼容模式是否开启?
I added the url to the list compatibility view settings. But when I do
我将 url 添加到列表兼容性视图设置中。但是当我这样做时
navigator.userAgent
in developer tools, it returns
在开发人员工具中,它返回
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; rv:11.0) like Gecko
Mozilla/5.0(Windows NT 6.3;WOW64;Trident/7.0;.NET4.0E;.NET4.0C;.NET CLR 3.5.30729;.NET CLR 2.0.50727;.NET CLR 3.0.30729;rvInfoPath.3; :11.0) 像壁虎
Looking at the microsoft website (http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx), it says
查看微软网站(http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx),它说
The compatible ("compatible") and browser ("MSIE") tokens have been removed.
兼容(“兼容”)和浏览器(“MSIE”)令牌已被删除。
Any help on detecting whether a page is using compatibility view via javascript would be really helpful. Thanks in advance.
任何有关检测页面是否通过 javascript 使用兼容性视图的帮助都会非常有帮助。提前致谢。
回答by jaegs
SOLVED
解决了
While searching for an answer to this question myself, I found this solution from Nenad Bulatovic in another thread but his response wasn't marked as the correct answer. I tested this out in IE11 and downgrading to IE5 and found that it works for IE7-IE11, which is great. I wanted to share it here in case anyone else finds it useful.
在自己寻找这个问题的答案时,我在另一个帖子中找到了 Nenad Bulatovic 的这个解决方案,但他的回答没有被标记为正确答案。我在 IE11 中对此进行了测试并降级到 IE5,发现它适用于 IE7-IE11,这很棒。我想在这里分享它,以防其他人觉得它有用。
iecheck.js
浏览器
function trueOrFalse() {
return true;
}
function IeVersion() {
//Set defaults
var value = {
IsIE: false,
TrueVersion: 0,
ActingVersion: 0,
CompatibilityMode: false
};
//Try to find the Trident version number
var trident = navigator.userAgent.match(/Trident\/(\d+)/);
if (trident) {
value.IsIE = true;
//Convert from the Trident version number to the IE version number
value.TrueVersion = parseInt(trident[1], 10) + 4;
}
//Try to find the MSIE number
var msie = navigator.userAgent.match(/MSIE (\d+)/);
if (msie) {
value.IsIE = true;
//Find the IE version number from the user agent string
value.ActingVersion = parseInt(msie[1]);
} else {
//Must be IE 11 in "edge" mode
value.ActingVersion = value.TrueVersion;
}
//If we have both a Trident and MSIE version number, see if they're different
if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) {
//In compatibility mode if the trident number doesn't match up with the MSIE number
value.CompatibilityMode = value.TrueVersion != value.ActingVersion;
}
return value;
}
iecheck.html
iecheck.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing IE Compatibility Mode</title>
<script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">
var ie = IeVersion();
document.write("IsIE: " + ie.IsIE + "</br>");
document.write("TrueVersion: " + ie.TrueVersion + "</br>");
document.write("ActingVersion: " + ie.ActingVersion + "</br>");
document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>");
</script>
</body>
</html>
source: Detect IE10 compatibility mode
来源:检测IE10兼容模式
回答by Gavin.Paolucci.Kleinow
I wrote a JavaScript function, ie-truth, to do just this. How it works is that in IE 11 if compatibility mode is turned on, the User Agent string will contain the Trident version number for IE 11 (7.0) as well as the MSIE version number for an older version of IE (such as 7.0 for IE 7). This also applies to compatibility mode in older versions of IE.
我编写了一个 JavaScript 函数ie-truth来做到这一点。它的工作原理是,在 IE 11 中,如果打开兼容模式,则用户代理字符串将包含 IE 11 (7.0) 的 Trident 版本号以及旧版 IE(例如 IE 的 7.0)的 MSIE 版本号7)。这也适用于旧版本 IE 中的兼容模式。
回答by Kris Truyens
Add this to web.config file and application will overwrite user's setting.
将此添加到 web.config 文件,应用程序将覆盖用户的设置。
<configuration>
...
<system.webServer>
...
<httpProtocol>
<customHeaders>
<clear/>
<add name="X-UA-Compatible" value="IE=8; IE=9; IE=EDGE" />
</customHeaders>
</httpProtocol>
...
</system.webServer>
...
</configuration>
Place the "system.webServer" tag at the end of your web.config file just before the closing "configuration" tag. Additionally, you can add the X-UA-Compatible tag in IIS on your webserver by selecting your website and clicking on the HTTP Response Headers icon.
将“system.webServer”标记放在 web.config 文件末尾的“configuration”结束标记之前。此外,您可以通过选择您的网站并单击 HTTP 响应标头图标,在您的网络服务器上的 IIS 中添加 X-UA-Compatible 标签。
回答by geekdenz
I would recommend that one uses feature detection rather than indirectly querying the browser version. So, for example, if you require the HTML 5 history API feature, do something like:
我建议使用特征检测而不是间接查询浏览器版本。因此,例如,如果您需要 HTML 5 历史 API 功能,请执行以下操作:
if (window.history && window.history.pushState) {
console.log('This is a SUPPORTED browser');
} else {
console.log('NO! You require a browser that does X, please try installing ...');
}
回答by user1775054
A simple solution, try it in the console:
一个简单的解决方案,在控制台中尝试一下:
if (typeof(window.getSelection) == "undefined") {
alert("Unsupported browser!");
} else {
alert("Supported browser");
}
Will detect any IE less than IE9 (including compatibility view). Would also detect Chrome 4-14 according to caniuse.
将检测任何低于 IE9 的 IE(包括兼容性视图)。还会根据 caniuse 检测 Chrome 4-14。
IE 6-8: Supports selection events, but not window.getSelection() Ref: https://caniuse.com/#feat=selection-api
IE 6-8:支持选择事件,但不支持 window.getSelection() 参考:https: //caniuse.com/#feat=selection-api
回答by robocat
A reliable solutionyou can editthat works for Internet Explorer 8 to 11 (needs extra code to support < IE8).
一个可靠的解决方案,您可以编辑该作品的Internet Explorer 8到11(需要额外的代码来支持<IE8)。
Nastyside-effects (only if IE < 11 or documentMode < 11 -- ouch):
令人讨厌的副作用(仅当 IE < 11 或 documentMode < 11 -- 哎哟):
- This will cause problemswith
//@ sourceMappingURL=xx.js(e.g. in jQuery < 1.11, or other libraries that haven't updated to the newer//#format). - Apparently @cc_on dramatically slows down js evaluation (re Paul Irish who knows his stuff).
- 这将引起问题与
//@ sourceMappingURL=xx.js(例如jQuery中<1.11,或者说还没有更新为较新的其他库//#格式)。 - 显然@cc_on 大大减慢了 js 评估(重新保罗爱尔兰谁知道他的东西)。
The basic reason it works is that:
它起作用的基本原因是:
var ieRealVersion = Function('return /*@cc_on @_jscript_version@*/;')();
returns the real version of IE regardless of compatibility mode. Notes:
无论兼容模式如何,都返回 IE 的真实版本。笔记:
- ieRealVersion is undefined for IE11 in IE11 mode, but document.documentMode == 11 in that case.
- ieRealVersion is 5.8 for IE8.
- ieRealVersion is 5.7 for IE7, and also 5.7 for IE6 SP3. However documentMode is undefined so you need extra code to detect actual browser (fairly trivial, but I don't support IE6/7 any more, so I haven't bothered to code that).
- This is a reliable way to sniff for Internet Explorer 6-10 that I have used for many years with no problems (but which has caused problems for others due to @cc_on).
- IE11 模式下的 IE11 未定义 ieRealVersion,但在这种情况下 document.documentMode == 11。
- IE8 的 ieRealVersion 是 5.8。
- ieRealVersion 对于 IE7 是 5.7,对于 IE6 SP3 也是 5.7。但是 documentMode 是未定义的,所以你需要额外的代码来检测实际的浏览器(相当琐碎,但我不再支持 IE6/7,所以我没有费心去编码)。
- 这是一种可靠的 Internet Explorer 6-10 嗅探方法,我已经使用了很多年,没有出现任何问题(但由于 @cc_on 导致其他人出现问题)。
回答by Lance Leonard
Per the user agentarticle of the IE Compatibility Cookbook, there is a way you can tell that compat mode is enabled, but only when the user-agent string cooperates.
根据IE Compatibility Cookbook的用户代理文章,有一种方法可以告诉您是否启用了兼容模式,但前提是用户代理字符串配合使用。
Specifically, if the browser token says MSIE 7.0and the Trident token says Trident/7.0, that's a pretty clear indication. Still, the changes to the UA string since IE11 RTM show that you cannot--and should not--rely on it as a predicable resource in future versions.
具体来说,如果浏览器标记为MSIE 7.0并且 Trident 标记为Trident/7.0,这是一个非常明确的指示。尽管如此,自 IE11 RTM 以来对 UA 字符串的更改表明您不能——也不应该——依赖它作为未来版本中的可预测资源。
To learn more about the individual tokens, see the Understanding user-agent stringstopic. (It's not entirely current, but what is there seems relevant to your interests.)
要了解有关各个令牌的更多信息,请参阅了解用户代理字符串主题。(它不完全是最新的,但似乎与您的兴趣相关。)
Hope this helps...
希望这可以帮助...
-- Lance
——兰斯

