Html 使用“X-UA-Compatible”为 IE8 模拟 IE7,但不适用于 IE9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3413629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:59:46  来源:igfitidea点击:

Emulate IE7 for IE8 but not for IE9 using "X-UA-Compatible"

htmlinternet-explorersvgvmlx-ua-compatible

提问by Urjan

I have a website depending on vector drawing, for Internet Explorer I'm using VMLand for other browsers I'm using SVG. IE8 however, doesn't have support for neither without falling back to IE7-mode which has VML.

我有一个依赖矢量绘图的网站,对于 Internet Explorer,我使用的是VML,而对于其他浏览器,我使用的是SVG。然而,IE8 不支持两者,除非退回到具有 VML 的 IE7 模式。

Therefore I'm including <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />.

因此我包括<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />.

The problem (well, actually a good thing) is that IE9 now has support for SVG so I don't want it to fall back to IE7-mode which has much worse performance and compatibility. How do I tell only IE8 to fall back to IE7-mode but let IE9 stay in IE9-mode?

问题(嗯,实际上是一件好事)是 IE9 现在支持 SVG,所以我不希望它回退到性能和兼容性更差的 IE7 模式。我如何只告诉 IE8 回退到 IE7 模式,但让 IE9 保持在 IE9 模式?

Right now I'm doing a server side check on the agent whether to include the EmulateIE7-string in the head or not but I want to avoid this as far as it's possible.

现在我正在对代理进行服务器端检查是否在头部包含 EmulateIE7 字符串,但我想尽可能避免这种情况。

回答by robocat

I just had a play and found the following works for me:

我刚刚玩了一场戏,发现了以下适合我的作品:

<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" >

That is with a comma not a semi colon!

那是逗号而不是分号!

I haven't looked at the spec, but the format is similar to content="IE=7,chrome=1" that works for Chrome Frame. I also found that content="IE=7,9" works but I suspect that is not a correct format.

我没有看过规范,但格式类似于适用于 Chrome Frame 的 content="IE=7,chrome=1"。我还发现 content="IE=7,9" 有效,但我怀疑这不是正确的格式。

Edit:

编辑:

Beware of a serious problem if your page is in a iframe. If you use the above in a framed page where the parent is in anymode less than IE9 strict, then IE9 will fall back to IE8 mode(ignoring the IE=7 request!). Any known workarounds welcome :) Might not be relevant to IE11.

如果您的页面在 iframe 中,请注意严重问题。如果您在父处于任何低于 IE9 严格模式的框架页面中使用上述内容,那么IE9 将回退到 IE8 模式(忽略 IE=7 请求!)。欢迎任何已知的解决方法:) 可能与 IE11 无关。

The above seems to be a side effect of the by design feature, that iframes (and I presume frames) are either all in IE9 mode, or all are less than IE9 mode. One can never mix IE9 frames with < IE9 frames, see MS issues #599022and #635648.

以上似乎是设计功能的副作用,即 iframe(我假设框架)要么全部处于 IE9 模式,要么全部小于 IE9 模式。永远不能将 IE9 框架与 < IE9 框架混合使用,请参阅 MS 问题#599022#635648

Edit 2:

编辑2:

Beware that IE11 only supports "IE=edge" (not IE=11), and that using IE=edge has significant effects upon IE functionality (including the user agent).

请注意 IE11 仅支持“IE=edge”(不支持 IE=11),并且使用 IE=edge 对 IE 功能(包括用户代理)有显着影响。

Edit 3:

编辑3:

  • Fantastic flow chartexplaining how IE works out what mode to use for IE9
  • IE=edgeis supported by IE8 through to IE11.
  • The Meta tag takes precedence over the HTTP header (which can be used instead of the meta tag)
  • Some more X-UA-Compatible info for IE10.

Edit 4:

编辑4:

X-UA-Compatiblewas removed from the Microsoft Edge browser. Only Internet Explorer has the compatibility modes. Beware that if you are using the WebView within an App on Windows Phone 10, then you are still using IE11 (not Edge).

X-UA-Compatible已从 Microsoft Edge 浏览器中删除。只有 Internet Explorer 具有兼容模式。请注意,如果您在 Windows Phone 10 上的应用程序中使用 WebView,那么您仍在使用 IE11(而不是 Edge)。

Also for a variety of reasons you cannot trust the user agent to tell you the correct compatibility level, instead use document.documentModefrom JavaScript.

同样出于各种原因,您不能相信用户代理会告诉您正确的兼容性级别,而是使用document.documentModeJavaScript。

Edit 5:

编辑5:

IE11 still needs X-UA-Compatibleset to IE=EDGEfor some corner cases e.g. a customer using IE11 from ActiveX (as WebView within a wrapper application) can drop IE11 back to IE7 mode if you don't set this.

对于某些极端情况,IE11 仍然需要X-UA-Compatible设置为IE=EDGE,例如,如果您不设置此项,则使用 ActiveX 中的 IE11(作为包装应用程序中的 WebView)的客户可以将 IE11 降回 IE7 模式。

回答by squareman

The dual mode mentioned by someone else shouldwork (but doesn't as shown by Microsoft) and is the closest thing I've seen in MS documentation that should work as described. There's an update below that shows the proper form the meta attribute value should take.

其他人提到的双模式应该可以工作(但不像Microsoft 所显示的那样),并且是我在 MS 文档中看到的应该按描述工作的最接近的东西。下面有一个更新,显示了元属性值应该采用的正确形式。

So if you use this:

所以如果你使用这个:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9">

Unfortunately, what you will get is IE8 rendering as IE8 because of the fuzzy version vectoring that the x-ua-compatible engine does. See this document: Defining Document Compatibility: Understanding Content Attribute Valueson MSDN. In that section, you'll see that in the first half, they define any version vector defined as larger than the current browser version will be interpreted as the largest available rendering engine. Therefore, emulateIE9 get's translated down to emulateIE8. Stupid.

不幸的是,由于 x-ua 兼容引擎所做的模糊版本矢量化,您将获得 IE8 渲染为 IE8。请参阅此文档: 定义文档兼容性:了解MSDN 上的内容属性值。在该部分中,您将看到在前半部分,他们定义了大于当前浏览器版本的任何版本向量,将被解释为最大的可用渲染引擎。因此,emulateIE9 被翻译成emulateIE8。愚蠢的。

Then, in the same breath practically, they talk about using multiple version vectors as in the code snippet above to exclude a particular engine. But because of the fuzzy version logic, that would never work. Ah, Microsoft. Fail again.

然后,实际上,他们同时讨论了使用多个版本向量(如上面的代码片段中所示)来排除特定引擎。但由于版本逻辑模糊,这永远行不通。啊,微软。又失败了。

The reason why using CCs around the meta won't work, is that the browser must have chosen a rendering engine by the time it hits a CC. The x-ua meta must come before anything else in the header except other metas or the title according to MS's own documentation.

围绕元使用 CC 不起作用的原因是浏览器必须在遇到 CC 时选择了渲染引擎。根据 MS 自己的文档,x-ua 元必须出现在标题中除其他元或标题之外的任何其他内容之前。

If anyone can figure this out, I'm all ears because I'm desperate to exclude IE8 from support while including IE9.

如果有人能解决这个问题,我会全力以赴,因为我非常希望在支持 IE9 的同时排除 IE8。

IMPORTANT UPDATE:

重要更新

Robocatpoints out, using a comma instead of a semi-colon as Micrsoft shows is the correct way of doing this. I tested it and it worked for me. I've updated my test page.

Robocat指出,使用逗号而不是分号(如 Micrsoft 所示)是正确的做法。我测试了它,它对我有用。我已经更新了我的测试页面

So the correctform is this (as suggested by robocat):

所以正确的形式是这样的(如 robocat 所建议的):

<meta http-equiv="X-UA-Compatible" content="IE=7, IE=9">

The incorrectform is this (as suggested by Microsoft):

不正确的形式是这样的(由微软的建议):

<meta http-equiv="X-UA-Compatible" content="IE=7; IE=9">

回答by Deepfreezed

I have so far used all these, nothing works on IE9:

到目前为止,我已经使用了所有这些,在 IE9 上没有任何效果:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >
<meta http-equiv="X-UA-Compatible" content="IE=8" >

<httpProtocol>
        <customHeaders>
          <clear />
          <add name="X-UA-Compatible" value="IE=EmulateIE8" />
        </customHeaders>
    </httpProtocol>

this is so frustrating, none of these meta tags seems to be working. Microsoft, What is so difficult in supporting stuff you say should work in your documentation? we have to spend hours doing browser wars. You are wasting everyone's time.

这太令人沮丧了,这些元标记似乎都不起作用。微软,支持你说应该在你的文档中使用的东西有什么困难?我们必须花费数小时进行浏览器大战。你在浪费大家的时间。

回答by J F

If you want IE 8 to use IE7 standards and IE 9 to use IE9 standards this one worked for me:

如果您希望 IE 8 使用 IE7 标准而 IE 9 使用 IE9 标准,那么这个对我有用:

<meta http-equiv="X-UA-Compatible" content="IE=7, IE=9">

For IE9 this gives me IE 9 compatibility mode with IE 9 standards. For IE8 this gives me Browser Mode IE8 Document Mode IE7 Standards

对于 IE9,这为我提供了与 IE 9 标准的 IE 9 兼容模式。对于 IE8,这给了我浏览器模式 IE8 文档模式 IE7 标准

回答by Pichet Thantipiputpinyo

This one works for IE9 to me.

这个对我来说适用于 IE9。

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>

回答by Alex

Wow Microsoft have really created a nightmare here. We're going to be talking about this well into the future!

哇,微软真的在这里制造了一场噩梦。我们将在未来谈论这个!

Anyway this works for me.

无论如何这对我有用。

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9" />

回答by Jon Robson

I think what you need is:

我认为你需要的是:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7; IE=EmulateIE9">

according to http://blogs.msdn.com/b/ie/archive/2010/06/16/ie-s-compatibility-features-for-site-developers.aspxas it states this as "... an example that combines values so that IE8 renders a webpage in IE7 Standards mode while IE9 renders the webpage in IE9's Standards mode:"

根据 http://blogs.msdn.com/b/ie/archive/2010/06/16/ie-s-compatibility-features-for-site-developers.aspx ,因为它指出这是“......一个例子组合值,以便 IE8 以 IE7 标准模式呈现网页,而 IE9 以 IE9 标准模式呈现网页:”

However I for one can't get this to work.

但是,我无法让它发挥作用。

回答by Sruly

<!--[if IE 8]>
  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<![endif]-->

It's called conditional comments
http://en.wikipedia.org/wiki/Conditional_comment

它被称为条件评论
http://en.wikipedia.org/wiki/Conditional_comment