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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5825385/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:04:36  来源:igfitidea点击:

JavaScript: Can I detect IE9 if it's in IE7 or IE8 compatibility mode?

javascriptinternet-explorer-9ie8-compatibility-mode

提问by Dee2000

I need to know if the browser that's identifying itself via user agent string as being IE7 or IE8 is really those browsers, or whether it's IE9 in 7 or 8 compatibility mode.

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

From what I can see in the user agent string, an IE9 in IE7 compatibility mode, provides an identical string to a real IE7. Is there an extra property/element/object that I can test to see if it's "really" IE9 in disguise?

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

I assume the document mode won't help as the page my script is loaded into could either be forcing quirks or forcing a specific setting.

我认为文档模式无济于事,因为我的脚本加载到的页面可能会强制怪癖或强制特定设置。

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.

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



Edited to add…

编辑添加…

OK, I see where I was going wrong now. I was using the "Browser Mode" dropdown and switching it to IE8 and IE7 and thinking this was "IE8 compatibility mode" and "IE7 compatibility mode" respectively. This is of course not true. The developer tools' Browser mode really is switching it to "be like" those old browsers, so it's only right that the original useragent strings be reported.

好的,我现在知道我哪里出错了。我正在使用“浏览器模式”下拉菜单并将其切换到 IE8 和 IE7,并认为这分别是“IE8 兼容模式”和“IE7 兼容模式”。这当然不是真的。开发人员工具的浏览器模式确实将其切换为“像”那些旧浏览器,因此报告原始用户代理字符串是正确的。

If I leave the browser mode in IE9 or IE9 compatibility and try the document mode dropdown variants instead, then I do in fact get "Trident/5.0" present in all 8 combinations (two browser modes and 4 document modes). I just need to steer clear of choosing browser mode IE7 and IE8 because they really are (simulated) IE7 and IE8.

如果我将浏览器模式保留在 IE9 或 IE9 兼容性中并尝试使用文档模式下拉变体,那么我实际上会在所有 8 种组合(两种浏览器模式和 4 种文档模式)中都出现“Trident/5.0”。我只需要避开选择浏览器模式 IE7 和 IE8,因为它们确实是(模拟)IE7 和 IE8。

So there's no way a page, a non-developer user, a meta tag, or Microsoft's compatibility list will be able to put IE9 into this unidentifiable state.

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

Just using if(navigator.userAgent.indexOf("Trident/5")>-1)will be sufficient.

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

Don't worry, this isn't for styles, formatting, logic or page content. I use feature detection for those things. I just need to detect IE9 (regardless of what mode it's in) and make a non-page content decision on that.

别担心,这不适用于样式、格式、逻辑或页面内容。我对这些事情使用特征检测。我只需要检测 IE9(不管它处于什么模式)并对此做出非页面内容决定。

Thanks for steering me towards the answer with your suggestions and links.

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

采纳答案by Dennis G

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.

实际上在IE7兼容模式下,IE9的用户代理字符串是不同的,所以这将是区分不同IE版本的最好方法之一。

Introducing IE9's User Agent String:

介绍 IE9 的用户代理字符串

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.

与 IE8 类似,IE9 的兼容性视图将映射到 IE7 标准模式,并且在兼容性视图中 IE9 的 UA 字符串将是:

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

在兼容性视图中,IE9 通过应用程序版本号 (Mozilla/4.0) 和版本令牌 (MSIE 7.0) 将自身报告为 IE7。这样做是为了兼容性。递增的 Trident 令牌,从“Trident/4.0”到“Trident/5.0”,允许网站区分在 Compat View 中运行的 IE9 和在 Compat View 中运行的 IE8

(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.

(重点是我加的)。因此,用户代理字符串与它报告自己是“Mozilla/4.0”和 MSIE 7.0 相同,但 IE9 将始终是 Trident/5.0 - 无论是 MSIE 7.0、MSIE 8.0 还是 MSIE 9.0。

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

实际上你应该看看这个很棒的汇编:浏览器 ID(用户代理)字符串或者更好的useragentstrings.com

回答by Yuhong Bao

document.documentModeis the best way for document mode.

document.documentMode是文档模式的最佳方式。

回答by Greg

IE7 doesn't contain any information on Trident

IE7 不包含任何关于 Trident 的信息

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

IE8 contains this string: "Trident/4.0"

IE8 包含此字符串:“Trident/4.0”

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

IE9 contains this string: "Trident/5.0"

IE9 包含此字符串:“Trident/5.0”

IE9 in compatability mode:

兼容模式下的IE9:

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

IE9 in normal mode:

正常模式下的IE9:

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

回答by Dr.Molle

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.

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

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

检查例如style.opacity,它是在 IE9 中引入的,无论兼容模式如何都可用:

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

回答by Pa?out

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

有时需要从服务器变量中读取用户代理字符串,而不是从 javascript 导航器对象中读取。

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)"

  • ASP经典,IE11

    • 客户端 javascript, navigator.userAgent: " Mozilla/5.0(兼容;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.0729;媒体中心 PC 6.0;.NET4.0C;.NET4.0E;BOIE9;ENUS)

    • 服务器 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经典,IE11兼容模式

    • 客户端 javascript, navigator.userAgent: " Mozilla/5.0(兼容;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.0729;媒体中心 PC 6.0;.NET4.0C;.NET4.0E;BOIE9;ENUS))

    • 服务器 ASP,Request.ServerVariables("HTTP_USER_AGENT"):" Mozilla/4.0(兼容;MSIE 7.0;Windows NT 6.1;WOW64;Trident/7.0;SLCC2;.NET CLR 2.0.50727;.NET CLR 3.5.30729; CLR 3.0.30729;媒体中心 PC 6.0;.NET4.0C;.NET4.0E;BOIE9;ENUS)

回答by TylerY86

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

来自https://stackoverflow.com/a/29288153/2879498

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

假设您有一个 ID 为 compat-warning 的隐藏元素:

Javascript w/ jQuery:

带有 jQ​​uery 的 Javascript:

$(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.

检测和警告,您抵御兼容性地狱的第一道也是最后一道防线。