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

Mor*_*eng 13 javascript browser standards render mode

我知道现代浏览器通常有两种渲染模式:标准模式和怪异模式.浏览器检测标题DocType.

问题是如何在运行时检测当前页面的渲染模式.有没有Firebug工具呢?

scu*_*ffe 22

在IE8之前:

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

对于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);
Run Code Online (Sandbox Code Playgroud)

请注意,为了获得IE8新的"标准模式默认"行为的好处,您需要在IE8标准模式下进行渲染.

这种模式会影响你的HTML + CSS的渲染,以及在修复了JavaScript的方法,如document.getElementById( id );.setAttribute( name, value );