DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
IE8 文件相容性詳解(2):指定模式、伺服器設定與 documentMode 判定
編輯:CSS詳解     

在頁面中指定文件模式

做法是在網頁裡放入一個 meta 元素,以 X-UA-Compatible 的 http-equiv 標頭宣告。以下範例模擬 IE7:

<html>
<head>
  <!-- Mimic Internet Explorer 7 -->
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  <title>My Web Page</title>
</head>
<body>
  <p>Content goes here.</p>
</body>
</html>

content 會依指定模式而變:模擬 IE7 用 IE=EmulateIE7,也可用 IE=5、IE=7、IE=8 直接挑一種模式;寫成 IE=edge 則是要求 IE8 使用它支援的最高模式。

標頭不分大小寫,但必須出現在 head 區段中除 title 與其他 meta 元素之外的所有元素之前。

由伺服器設定預設模式

網站管理者可以為整站定義自訂標頭。以下 web.config 讓 IIS 自動以 IE7 mode 編譯所有網頁:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear/>
        <add name="X-UA-Compatible" value="IE=EmulateIE7"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

個別頁面若也寫了 meta 標頭,其優先權高於伺服器設定。關於自訂標頭的細節,請參考 IIS 與 Apache 的官方文件。

如何判定目前生效的模式

使用 document 物件的 documentMode 屬性。在 IE8 位址列輸入以下程式碼,即可看到目前頁面的文件模式:

javascript:alert(document.documentMode);

它會回傳對應的數值,例如頁面指定為 IE8 mode 時回傳 "8"。

IE6 時代的 compatMode 並不適用於 IE8 引入的 documentMode;現有使用 compatMode 的程式雖仍能運作,但應逐步遷移。

若需要同時相容舊版 IE,可用這段程式:

engine = null;
if (window.navigator.appName == "Microsoft Internet Explorer") {
  if (document.documentMode) {          // IE8
    engine = document.documentMode;
  } else {                              // IE5-7
    engine = 5;                         // 先假設 quirks mode
    if (document.compatMode) {
      if (document.compatMode == "CSS1Compat") engine = 7; // standards mode
    }
  }
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved