JavaScript:我可以检测IE9是否在IE7或IE8兼容模式下? [英] JavaScript: Can I detect IE9 if it's in IE7 or IE8 compatibility mode?

查看:176
本文介绍了JavaScript:我可以检测IE9是否在IE7或IE8兼容模式下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道通过用户代理字符串识别自己的IE7或IE8的浏览器是否真的是那些浏览器,或者它是否是7或8兼容模式下的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.

从我在用户代理字符串中看到的,IE7兼容模式下的IE9,为真正的IE7提供了相同的字符串。是否有一个额外的属性/元素/对象,我可以测试它是否真的伪装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?

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

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.

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

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.

编辑添加...

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

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.

如果我离开在IE9或IE9兼容性的浏览器模式和尝试文档模式下拉变体,然后我实际上在所有8种组合(两种浏览器模式和4种文档模式)中存在Trident / 5.0。我只需要避免选择浏览器模式IE7和IE8,因为它们确实是(模拟)IE7和IE8。

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.

所以没有一个页面,一个非开发者用户,元标记或Microsoft的兼容性列表将能够将IE9置于这种无法识别的状态。

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.

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

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

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.

推荐答案

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

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.

介绍IE9的用户代理字符串


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

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)

在兼容性视图中,IE9通过appli报告
本身为IE7阳离子
版本号(Mozilla / 4.0)和
版本令牌(MSIE 7.0)。为了兼容性,这是
一个递增的
三叉戟令牌,从'Trident / 4.0'到
'Trident / 5.0',允许网站以
区分IE9中运行的
Compat View和IE8运行在Compat
查看

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.

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

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

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

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

这篇关于JavaScript:我可以检测IE9是否在IE7或IE8兼容模式下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆