IE6 對 CSS 的支援停留在十幾年前的水準,為了它,開發者往往得額外背上一整套 hack、多寫三成程式碼,還要接受版面在不同瀏覽器下歪掉。與其默默承受,不如讓使用者知道「有更好的選擇」。
做法是這樣的:把一段提示區塊放進頁面,用 CSS 讓它只在 IE6 顯示,其他現代瀏覽器完全看不到。再把網站原本的所有內容包進另一層容器,讓它在 IE6 下隱藏。這樣一來,仍在使用 IE6 的訪客會看到一段簡潔的升級建議與下載連結,其他人則毫無感覺。
整段程式碼的核心只有兩個宣告:
.IE { display: none; } /* 預設隱藏提示 */
* html .IE { display: block; } /* 只有 IE6 會讀懂這個選擇器 */
.normal { _display: none; } /* 下底線 hack:只有 IE6 隱藏正文 */
* html 這個選擇器是 IE6 獨有的解析 bug,其他瀏覽器(包含 IE7 以上)都不會成立;而 _ 前綴同樣只有 IE6 認得。兩個 hack 搭配起來,就能做到精準的版本隔離,不需要任何 JavaScript 判斷。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>該升級瀏覽器了</title>
<style type="text/css">
.text { text-align: center; }
.text a {
font-size: 16px;
color: #333;
line-height: 180%;
text-decoration: none;
display: inline-block;
}
.text a:hover { color: red; text-decoration: underline; }
.fackIe a {
font-size: 28px;
font-weight: bold;
color: #fff;
text-decoration: none;
height: 50px;
line-height: 50px;
width: 100%;
text-align: center;
background: red;
display: block;
}
.fackIe a:hover { text-decoration: underline; color: #000; }
.normal {
_display: none;
font-size: 24px;
color: #666;
font-weight: bold;
text-align: center;
padding-top: 50px;
}
.IE { display: none; }
* html .IE { display: block; }
</style>
</head>
<body>
<div class="IE">
<div class="fackIe">
<a title="點擊升級瀏覽器" href="https://www.microsoft.com/zh-cn/edge">
你正在使用 IE6,建議升級到現代瀏覽器
</a>
</div>
<div class="text">
<p style="color:red; font-weight:bold;">這個網站的設計使用了較新的網頁標準,</p>
<p>IE6 已無法正確呈現,建議改用 Edge、Chrome、Firefox 或 Safari。</p>
<p>升級之後,你會看到更完整的版面、更快的載入速度,以及更安全的瀏覽體驗。</p>
</div>
</div>
<div class="normal">
這裡放網站原本的內容。
</div>
</body>
</html>
第一,把語氣放軟。很多使用者其實並不知道自己用的是舊版瀏覽器,公司電腦的既定環境更是無從選擇。一段說明清楚、附上連結的提示,效果遠勝於指責。
第二,別把整站藏起來。若你的訪客中有相當比例受舊環境限制(例如政府或醫療單位),直接隱藏內容等於拒絕服務。比較友善的做法是保留內容,只在頁首加一條細長的提示橫幅。
第三,連結要指向有效位置。微軟早年提供的 IE8 安裝檔連結早已失效,請改為指向目前仍在維護的瀏覽器官方下載頁。