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

发布于 2024-11-03 15:59:05 字数 868 浏览 0 评论 0原文

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

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

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

我希望 IE9 能够拥有一些存在且可测试的属性,无论它处于 7、8 还是 9 模式。


已编辑添加...

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

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

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

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

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

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

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.

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?

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.


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.

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.

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.

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

当爱已成负担 2024-11-10 15:59:05

实际上,当在 IE7 兼容模式下运行时,IE9 的用户代理字符串是不同的,因此这将是区分不同 IE 版本的最佳方法之一。

IE9的用户介绍代理字符串

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

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

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

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

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

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

吾性傲以野 2024-11-10 15:59:05

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

document.documentMode is the best way for document mode.

夜夜流光相皎洁 2024-11-10 15:59:05

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

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

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

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

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

IE9 兼容模式:

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

IE9 正常模式:

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

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)
心碎的声音 2024-11-10 15:59:05

我希望 IE9 能够拥有一些存在且可测试的属性,无论它处于 7、8 还是 9 模式。

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

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

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]>
盗心人 2024-11-10 15:59:05

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

比较差异:

  • ASP classic、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.0.30729;媒体中心 PC 6.0;.NET4.0E;BOIE9;

    • 服务器 ASP,Request.ServerVariables("HTTP_USER_AGENT"):“Mozilla/5.0(Windows NT 6.1;WOW64;Trident/7.0;BOIE9;ENUS;rv:11.0),如 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.0.30729;媒体中心 PC 6.0;.NET4.0E;BOIE9;

    • 服务器 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;.NET CLR 3.0.30729;.NET4.0C;.NET4.0E;


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

静待花开 2024-11-10 15:59:05

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

假设您有一个 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();
});

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文