JavaScript: 如果 IE9处于 IE7或 IE8兼容模式,我能检测到它吗?

我需要知道通过用户代理字符串标识自己为 IE7或 IE8的浏览器是否真的是那些浏览器,或者它是7或8兼容模式下的 IE9。

从用户代理字符串中我可以看到,IE7兼容模式下的 IE9提供了与真正的 IE7相同的字符串。是否有一个额外的属性/元素/对象,我可以测试看看它是否“真的”IE9伪装?

我假设文档模式不会有帮助,因为我的脚本加载到的页面可能是强制性的怪癖或强制性的特定设置。

我希望 IE9将有一些属性,存在和可测试的,无论它是在7,8或9模式。


编辑添加..。

好吧,我知道我哪里做错了。我使用“浏览器模式”下拉菜单,将其切换到 IE8和 IE7,并认为这是“ IE8兼容模式”和“ IE7兼容模式”分别。这当然不是真的。开发工具的浏览器模式实际上正在将其转换为“类似”那些老式浏览器,因此只有报告原始用户代理字符串才是正确的。

如果我将浏览器模式设置为 IE9或 IE9兼容,并尝试使用文档模式下拉列表变体,那么事实上我将得到“ Trident/5.0”,这8种组合(两种浏览器模式和4种文档模式)。我只是需要避开选择浏览器模式 IE7和 IE8,因为他们真的是(模拟) IE7和 IE8。

因此,一个页面、一个非开发人员用户、一个元标记或微软的兼容性列表都不可能将 IE9置于这种无法识别的状态。

只要使用 if(navigator.userAgent.indexOf("Trident/5")>-1)就足够了。

别担心,这不是针对样式、格式、逻辑或页面内容的。我用的是特征提取。我只需要检测 IE9(不管它处于什么模式) ,然后做一个非页面内容的决定。

感谢您的建议和链接引导我找到答案。

85807 次浏览

I'm hoping that IE9 will have some property that exists and is testable regardless of whether it's in 7, 8 or 9 mode.

Check e.g. for style.opacity, it was introduced in IE9 and is available regardless of the compatibility-mode:

<![if IE]>
<script>
if(typeof document.documentElement.style.opacity!='undefined')
{
//this must be at least IE9
}
</script>
<![endif]>

Actually the user agent string is different for IE9 when being run in IE7 compatibility mode, so this would be one of the best ways to distinguish between different IE versions.

Introducing IE9’s User Agent String:

Similar to IE8, IE9’s Compatibility View will map to IE7 Standards Mode, and IE9’s UA string when in Compatibility View will be:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)

In Compatibility View, IE9 reports itself as IE7 through the application version number (Mozilla/4.0) and version token (MSIE 7.0). This is done for compatibility. An incremented Trident token, from ‘Trident/4.0’ to ‘Trident/5.0’, allows websites to distinguish between IE9 running in Compat View and IE8 running in Compat View.

(emphasis added by me). So the user agent string is the same as it reports itself being "Mozilla/4.0" and MSIE 7.0, but IE9 will always be Trident/5.0 - no matter whether it says MSIE 7.0, MSIE 8.0 or MSIE 9.0.

Actually you should check out this great compilation: Browser ID (User-Agent) Strings or even better useragentstrings.com

document.documentMode is the best way for document mode.

IE7 doesn't contain any information on Trident

User-Agent : Mozilla/4.0 (compatible; MSIE 7.0)

IE8 contains this string: "Trident/4.0"

User-Agent : Mozilla/4.0 (compatible; MSIE 8.0; Trident/4.0)

IE9 contains this string: "Trident/5.0"

IE9 in compatability mode:

User-Agent : Mozilla/4.0 (compatible; MSIE 7.0; Trident/5.0)

IE9 in normal mode:

User-Agent : Mozilla/5.0 (compatible; MSIE 9.0; Trident/5.0)

It is sometimes necessary to read the user Agent string from server Variable, not from javascript navigator object.

Compare the differences:

  • ASP classic, IE11

    • client javascript, navigator.userAgent: "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BOIE9;ENUS)"

    • server ASP, Request.ServerVariables("HTTP_USER_AGENT"): "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; BOIE9;ENUS; rv:11.0) like Gecko"

  • ASP classic, IE11 Compatibility mode:

    • client javascript, navigator.userAgent: "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BOIE9;ENUS))"

    • server ASP, Request.ServerVariables("HTTP_USER_AGENT"): "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BOIE9;ENUS)"

From https://stackoverflow.com/a/29288153/2879498

Assuming you have a hidden element with the ID compat-warning:

Javascript w/ jQuery:

$(function(){
function showCompatWarning() {
$('#compat-warning')
.css('display','block')
.css('height','auto')
.show();
}
var tridentOffset = navigator.appVersion.indexOf('Trident/');
if ( tridentOffset === -1 ) return;
var jscriptVersion = 0;
/*@cc_on @*/
/*@if (@_jscript) jscriptVersion = @_jscript_version ; @*/;
/*@end @*/
var tridentVersion = parseInt(navigator.appVersion.substr(tridentOffset+8),10);
var guessIEVersion = tridentVersion + 4;
if (( document.documentMode && jscriptVersion && jscriptVersion < 10 && jscriptVersion !== document.documentMode ) ||
( document.compatMode && document.compatMode === 'BackCompat') ||
( document.documentMode && document.documentMode < 10 && document.documentMode != guessIEVersion ))
showCompatWarning();
});

Detection and warnings, your first and last lines of defense against compatibility hell.