[Web開發] IE8裡判斷當前網頁顯示模式

來源:互聯網
上載者:User

我們知道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); 來查看當前網頁的顯示模式。

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.