我們知道IE8 的一個重要更新就是加入了標準模式(standards mode)的顯示引擎,但IE8裡面仍然保留以前IE版本的顯示模式,比如Strict Mode 以及 Quirks mode, 我們統稱之為相容模式 (compatibility view)。
那麼如何判斷IE8 用什麼模式顯示當前網頁呢? IE8 裡面新加Javascript 函數 document.documentMode 能夠很好協助我們解決這個問題。
document.documentMode 的傳回值有3個,其含義如下:
5 表示老版本IE的Quirks mode.
7 表示老版本IE的Strict mode.
8 表示IE8的標準模式 standards mode.
document.documentMode 只有在IE8上有,對於老版本IE需要使用其他API。以下代碼可以讓你在所有版本IE下判斷顯示模式:
engine = null;<br />if (window.navigator.appName == "Microsoft Internet Explorer")<br />{<br /> // 當前瀏覽器是IE,下面判斷具體的顯示模式<br /> if (document.documentMode) // IE8<br /> engine = document.documentMode;<br /> else // IE 5-7<br /> {<br /> engine = 5; // quirks mode unless proven otherwise<br /> if (document.compatMode)<br /> {<br /> if (document.compatMode == "CSS1Compat")<br /> engine = 7; // standards mode<br /> }<br /> }<br /> alert("IE的當前顯示模式是" + engine);<br />}<br />
Tips:你可以在IE地址欄裡面輸入 javascript:alert(document.documentMode); 來查看當前網頁的顯示模式。