閱讀之前:
在看文章具體內容之前,希望你可以先打開IE8,打開http://www.taobao.com ,然後在網址列輸入:
javascript:alert(document.documentMode +navigator.userAgent);void(0)
看到結果後不知道你有沒覺得驚奇,反正我是覺得很奇怪的,因為頁面meta裡沒有兼容標記, fiddler查看http頭裡也沒有X-UA-Compatible標記,網站到底為什麼會運行在ie7模式下呢?
IE7相容模式與相容視圖
昨天在看口碑的一篇iframe高度自適應時,發現它給的demo與我本的demo在ie8中運行結果不一樣,在線的結果明顯跟在IETester7中一樣,也查看過,meta裡沒有兼容標記, fiddler查看http頭裡也沒有X-UA-Compatible標記,可是為什麼在線的結果與本地的會不一樣呢?這樣我就鬱悶了,再一看,發現在線demo在IE8中沒有顯示“兼容視圖”按鈕,想想肯定是這個的原因.
後來找到一篇文章Just The Facts: Recap of Compatibility View雖然沒解決問題,但內容不錯,分享一下,引用其中一段話:
Compatibility View and the X-UA-Compatible tag are not equivalent
Compatibility View is something you do on the client. It affects three things: the User Agent string, the Version Vector (used in evaluation of conditional comments), and what mode DOCTYPEs that trigger Standards map to The 8 Standards. -UA-Compatible <META> tag / header is something you use in page content / server-side and, when present, completely overrides Compatibility View settings on the client. It affects two things: the Version Vector and that mrigs that affects two。 map to. It can't affect the UA string as it's already too late to change that – the client's already made the GET request to the server (and it contains a UA string). What this means to devperselo is thatif if on the User Agent string, adding just the X-UA-Compatible tag (to cause IE8 to display your site in IE7 Standards mode) won't make your website compatible – you'll also need to strate your Usersite compatible – you'll also need to strate well.
意思是說,相容視圖有三個作用:
而meta標記只起兩個作用:1.版本,2.文檔類型
(這裡說meta標記不影響UserAgent應該是指服務端的ua,因為客戶端請求已經發送,並且包含了ua,所以不會影響,不過客戶端的ua還是受影響了,顯示ie7 )
更正:
(可能之前測試有誤)客戶端的ua也不影響,只會影響document.documentMode
另外:
<META> tag / header分別指:<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">和在服務端新增http頭,例如.net設定檔:
另外也可以到IIS或Apache加入http頭:
問題解決
到這裡了文章開始的問題還是沒有解決,昨晚無意間看到秦歌的一篇用doctype激活瀏覽器模式,中提到微軟的黑名單站點列表中也會啟用兼容模式,記得上面那文章第兩個評論中有一個:res://iecompat.dll/iecompatdata.xml(IE8 only哦),將這個地址輸入到ie8中會顯示:Compatibility View list,這樣在這個列表中終於找到口碑和淘寶的域名,所以我想這應該就是本文開始時問題的答案了,你也可以在這個列表中隨便找一個域名,打開,在地址欄輸入文章開始那那段javascript(ps:微軟這個列表也太多了吧,將這麼多站點弄成IE7。
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-UA-Compatible" value="IE=EmulateIE7" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>