javascript 如何获取浏览器“文档模式”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13121016/
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
How to get browser "Document Mode"
提问by Mikhail
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:
我正在开发某种 JavaScript 库。我导致了我有一些特定问题的问题:浏览器:IE8/IE9 和文档模式:IE7 我找到了解决方案,但我不想在所有情况下都使用它,只想在我有的时候使用它上述情况。我知道我可以使用以下方法识别浏览器:
return navigator.userAgent.toLowerCase().indexOf('MSIE 8') > -1;
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.
但是我以这种方式只识别浏览器版本而不是文档模式,并且当我有浏览器模式 IE8 和文档模式 IE 8 时,我不想使用我的解决方案。 有没有办法获得页面文档模式在 IE 中?提前致谢。
回答by Sam Thornton
You can use document.documentMode
to return exactly what document mode IE is using.
您可以使用document.documentMode
来准确返回 IE 正在使用的文档模式。
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).
这意味着如果您将浏览器模式设置为 IE9,但将文档模式设置为 IE8,它将返回 document.documentMode == 8(而 userAgent 字符串仍将显示为 IE9)。如果您的 JS 曾经包含样式更改,这将特别有用,因为它是决定 IE 如何呈现页面的文档模式,而不是浏览器模式。兼容模式实际上只是改变了文档模式(通常是 IE7)。
In the few cases I've needed to I've just used something like this to differentiate IE's:
在少数情况下,我需要使用这样的方法来区分 IE:
if (document.documentMode == 7) {
doSomethingIn.IE7_only;
} else {
doSomething.everwhereElse;
}
Hope that helps some.
希望能帮到一些人。
回答by KooiInc
I don't know how to retrieve the document mode1, but it may be wise to address the problem in a more basic way. Let's say you wanted to use document.querySelector
in your scripting. That would fail in IE8/document mode IE7 Standards. So an additional check for the existence of document.querySelector
itself would be the solution:
我不知道如何检索文档模式1,但以更基本的方式解决问题可能是明智的。假设您想document.querySelector
在脚本中使用。这将在 IE8/文档模式 IE7 标准中失败。因此,对其document.querySelector
自身存在的额外检查将是解决方案:
return ~navigator.userAgent.toLowerCase().indexOf('MSIE 8')
&& document.querySelector; //=> IE8 and Docmode IE7 => false
1Found a way to check for document mode: use document.documentMode
. It returns an integer (so 7 for document mode IE7 standards), 5 for Quirks mode
. It will be undefined
for non IE browsers.
1找到了一种检查文档模式的方法:使用document.documentMode
. 它返回一个整数(所以 7 用于文档模式 IE7 标准),5 用于Quirks mode
. 它将undefined
用于非 IE 浏览器。