阅读之前:
在看文章具体内容之前,希望你可以 先打开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 – IE8 Standards or IE7 Standards. The X-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 what mode DOCTYPEs that trigger Standards 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 developers is that if your site pivots 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 update your User Agent string detection logic as 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>