如何获得浏览器“文档模式” [英] How to get browser "Document Mode"

查看:132
本文介绍了如何获得浏览器“文档模式”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发某种JavaScript库。我导致问题,我有一些具体问题:
浏览器:IE8 / IE9和文档模式:IE7
我找到了解决方案,但我不想在所有情况下使用它,并且想要在我遇到上述情况时使用它。我知道我可以通过以下方式识别浏览器:

I am developing some kind of JavaScript library. And i cause the problem that i have some specific issues for: Browser : IE8 / IE9 and Document Mode : IE7 I found the solution, but i don't want to use it in all situation, and want to use it just when i have the situation described above. I know that I can recognize browser by using:

return navigator.userAgent.toLowerCase().indexOf('MSIE 8') > -1;

但我认识的只是浏览器版本而不是文档模式,我不想要例如,当我有浏览器模式IE8和文档模式IE 8时使用我的解决方案。
有没有办法在IE中获取页面文档模式?在此先感谢。

But i recognize just browser version in such way but not the document mode, and i don't want to use my solution when I have, for example, browser mode IE8 and document mode IE 8. Is there a way to get page document mode in IE? Thanks in advance.

推荐答案

您可以使用 document.documentMode 返回IE正在使用哪种文档模式。

You can use document.documentMode to return exactly what document mode IE is using.

这意味着如果你的浏览器模式设置为IE9,但你的文档模式设置为IE8,它将返回document.documentMode == 8 (而userAgent字符串仍将显示为IE9)。如果您的JS包含样式更改,这将特别有用,因为它是确定IE呈现页面的方式的文档模式,而不是浏览器模式。兼容模式实际上只是改变了文档模式(通常是IE7)。

Meaning that if you have your browser mode set to IE9, but your document mode to IE8 it will return document.documentMode == 8 (while the userAgent string will still show up as IE9). This is particularly useful if your JS ever includes styling changes as it is the document mode that determines how IE renders a page, not the browser mode. Compatibility mode really just changes the document mode (usually to IE7).

在少数情况下,我需要使用这样的东西来区分IE:

In the few cases I've needed to I've just used something like this to differentiate IE's:

if (document.documentMode == 7) {
    doSomethingIn.IE7_only;
} else {
    doSomething.everwhereElse;
}

希望有所帮助。

这篇关于如何获得浏览器“文档模式”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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