IE8+新增了一个获取的文档兼容性模式属性——documentMode。一般的获取文档兼容模式可以这么写:
1 | var p= document.documentMode; |
在各浏览器下值见下表:
浏览器 | IE6 | IE7 | IE8 | IE8 兼容模式 | IE9 | IE9 兼容模式 | IE10 | IE10 兼容模式 |
---|
document.documentMode | undefined | undefined | 8 | 7 | 9 | 7||8 | 10 | 7||8||9 |
除了这些取值,在IE的怪异模式下,document.documentMode的值是5;这个和document.compatMode属性有关,在html文档中没有指定DOCTYPE,在ie下就是怪异模式。
送一个document.compatMode属性,如果是标准模式,则document.compatMode的值等于”CSS1Compat“,如果是怪异模式,则document.compatMode的值等于”BackCompat“
if (document.compatMode == "CSS1Compat") { alert("Standards mode"); } else { alert("Quirks mode"); }
|
if
(
document
.
compatMode
==
"CSS1Compat"
)
{
alert
(
"Standards mode"
)
;
}
else
{
alert
(
"Quirks mode"
)
;
}
|
如果文档正在加载中,尚未完成加载,那么document.documentMode的值是0,所以你要确保文档加载完成才能取到正确的值。 开发人员会使用meta元素来指定X- UA-兼容的HTTP- EQUIV头的指定文档兼容性模式。
另:有了这个属性,判断ie各种版本也有了新方法,比较靠谱:
1 | var ieMode=document.documentMode; |
2 | var isIE=!!window.ActiveXObject; |
3 | var isIE6=isIE&&!window.XMLHttpRequest; |
4 | var isIE7=isIE&&!isIE6&&!ieMode||ieMode==7; |
5 | var isIE8=isIE&&ieMode==8; |
6 | var isIE9=isIE&&ieMode==9; |