舊專案在 IE9+ 顯示異常時,可透過 `X-UA-Compatible` 強制指定文件相容模式。本文說明 Meta 標籤、HTTP 標頭、IIS/web.config 設定與模式判斷方式。
| 模式值 | 渲染行為 |
|---|---|
| IE=5 | Quirks 模式(IE5 行為) |
| IE=7 | IE7 Standards 模式 |
| IE=8 | IE8 Standards 模式 |
| IE=EmulateIE7 | 依 `` 決定:有 DOCTYPE→IE7 Standards,無→IE5 Quirks(推薦) |
| IE=EmulateIE8 | 依 `` 決定:有→IE8 Standards,無→IE5 Quirks |
| IE=edge | 使用該瀏覽器最高可用模式(僅建議測試頁使用) |
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title>...</title>
</head>
⚠️ 必須置於 `
` 內**所有其他元素之前**(除 `適用於無法修改每頁 HTML 的舊專案。
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-UA-Compatible" value="IE=EmulateIE7" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
# .htaccess 或 httpd.conf
Header set X-UA-Compatible "IE=EmulateIE7"
Header unset X-UA-Compatible
靜態資源不需此標頭,建議排除。
let mode;
if (document.documentMode) mode = document.documentMode; // IE8+
else if (document.compatMode === 'CSS1Compat') mode = 7; // IE6-7 Standards
else mode = 5; // Quirks
強制相容模式只是暫時止血,長期應朝向標準相容、漸進增強方向重構。