JavaScript:如果处于 IE7 或 IE8 兼容模式,我可以检测到 IE9 吗?
我需要知道通过用户代理字符串将自身标识为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实际上,当在 IE7 兼容模式下运行时,IE9 的用户代理字符串是不同的,因此这将是区分不同 IE 版本的最佳方法之一。
IE9的用户介绍代理字符串:
(重点是我添加的)。因此,用户代理字符串与其报告自身为“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:
(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
是文档模式的最佳方式。document.documentMode
is the best way for document mode.IE7 不包含有关 Trident 的任何信息
IE8 包含此字符串:“Trident/4.0”
IE9 包含此字符串:“Trident/5.0”
IE9 兼容模式:
IE9 正常模式:
IE7 doesn't contain any information on Trident
IE8 contains this string: "Trident/4.0"
IE9 contains this string: "Trident/5.0"
IE9 in compatability mode:
IE9 in normal mode:
以下是IE9 的所有浏览器和文档模式的表格:
Here is a table of all Browser and Document modes for IE9:
我希望 IE9 能够拥有一些存在且可测试的属性,无论它处于 7、8 还是 9 模式。
例如检查 style.opacity,它是在 IE9 中引入,无论兼容模式如何都可用:
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:
有时需要从服务器变量而不是 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)"
来自 https://stackoverflow.com/a/29288153/2879498
假设您有一个 ID 为 compat-warning 的隐藏元素:
Javascript w/ jQuery:
检测和警告,您抵御兼容性地狱的第一道和最后一道防线。
From https://stackoverflow.com/a/29288153/2879498
Assuming you have a hidden element with the ID compat-warning:
Javascript w/ jQuery:
Detection and warnings, your first and last lines of defense against compatibility hell.