如何检测当前页面浏览器的渲染模式?

13
我知道现代浏览器通常有两种渲染模式:标准模式和怪异模式。浏览器会检测文档类型标头。
问题是如何在运行时检测当前页面的渲染模式。是否有任何 Firebug 工具来完成这个任务?
2个回答

22

在IE8之前:

alert('Page was rendered in ' +
  ((document.compatMode == 'CSS1Compat') ? 'Standards' : 'Quirks') + ' Mode.');

对于IE8:

var vMode = document.documentMode;
var rMode = 'IE5 Quirks Mode';
if(vMode == 8){
  rMode = 'IE8 Standards Mode';
} else if(vMode == 7){
  rMode = 'IE7 Strict Mode';
}
alert('Rendering in: ' + rMode);

请注意,要享受IE8新的“默认标准模式”的好处,您需要在IE8标准模式下呈现。

此模式会影响您的HTML+CSS的呈现以及JavaScript修复方法,例如document.getElementById(id);.setAttribute(name, value);


1
提示:使用以下代码作为URL创建收藏夹:_javascript:(function(){var vMode=document.documentMode;var rMode='IE5 Quirks Mode';if(vMode==8){rMode='IE8 Standards Mode';}else if(vMode==7){rMode='IE7 Strict Mode';}alert('Rendering in: '+rMode);})();_如果您使用此代码创建了收藏夹,可以将其命名为“检测呈现模式”。只需单击即可获取消息框。 - SimonSimCity
@SimonSimCity,有趣的是你提到了这个 - 我也有同样的东西,只不过我的叫做“渲染模式”。 - scunliffe
但是 IE 9 和 IE 10 呢?为什么不直接查看 document.compatMode 来判断它是 BackCompat(怪异模式)还是 CSS1Compat(标准兼容模式)呢? - nonopolarity
当这段代码编写时,IE9和IE10还不存在;-) 我会根据现在IE可以运行的完整模式矩阵更新代码。 - scunliffe

1
你还应该查看jQuery的jQuery.support。它会告诉你浏览器支持哪些标准(boxModel、opacity等)。

http://docs.jquery.com/Utilities/jQuery.support

jQuery.support.boxModel; //false in IE when in quirksmode, true otherwise.

此属性已在jQuery 1.8中移除。 - chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接