C# 如何在 Javascript 中以编程方式打开 IE9 兼容性视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9819026/
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-09 10:44:42  来源:igfitidea点击:

How to turn on IE9 Compatibility View programmatically in Javascript

c#javascriptasp.netinternet-explorer-9

提问by

I need to turn on IE compatibility programmatically.

我需要以编程方式打开 IE 兼容性。

I know this works in C# :

我知道这在 C# 中有效:

Page.Header.Controls.AddAt(0, new HtmlMeta { HttpEquiv = "X-UA-Compatible", Content = "IE=EmulateIE7" });

My problem is all my windows are displayed in a JS function: for instance:

我的问题是我所有的窗口都显示在一个 JS 函数中:例如:

function openRadWin(idAgir) {
    radopen("DemandesEnAttente_Estimate.aspx?id=" + idAgir, "RadWindow1");

}

So my question is : is there any ways to do the same thing in JS?

所以我的问题是:有没有办法在 JS 中做同样的事情?

Thanks in advance

提前致谢

采纳答案by VinayC

AFAIK, this is not possible. You can detect the compatibility modefrom JS but setting it is not possible to my knowledge.

AFAIK,这是不可能的。您可以从 JS检测兼容模式,但据我所知设置它是不可能的。

As for as your problem goes, typically you can use few solutions:

至于您的问题,通常您可以使用几种解决方案:

  1. If you are using Master pages in your site, add the meta header (<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">) in the master page.
  2. Similar to #1, if you are using a common base page class (a good and recommended practice) then you can infuse the meta header from the common base page to all your pages.
  3. Lastly, you can use IIS configuration to add the http header to all your response.
  1. 如果您在站点中使用母版页,请在母版页中添加元标题 ( <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">)。
  2. 与#1 类似,如果您使用的是公共基页类(一种良好且推荐的做法),那么您可以将元标头从公共基页注入所有页面。
  3. 最后,您可以使用 IIS 配置将 http 标头添加到您的所有响应中。

For example, for IIS7/IIS7.5, you can use web.config

例如对于IIS7/IIS7.5,可以使用web.config

<system.webServer>
  <httpProtocol>
     <customHeaders>
       <remove name="X-UA-Compatible" />
       <add name="X-UA-Compatible" value="IE=EmulateIE7" />
     </customHeaders>
  </httpProtocol>
</system.webServer>

I would suggest #1 or #2 - in case you don't have master page or base page class then perhaps its a good time to introduce the both.

我建议 #1 或 #2 - 如果您没有母版页或基页类,那么也许是介绍两者的好时机。

回答by Mike Chamberlain

You could try adding this HTML tag to the top of any page that requires compatibility:

您可以尝试将此 HTML 标记添加到任何需要兼容性的页面的顶部:

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

That way you shouldn't need a JavaScript solution. Of course, if you have the ability to change the HTML, then the best solution would be to fix the page so it doesn't require backwards compatibility.

这样你就不需要 JavaScript 解决方案了。当然,如果您有能力更改 HTML,那么最好的解决方案是修复页面,使其不需要向后兼容。

I believe that the only way to influence headers from the client side is when you request a page using an XmlHttpRequest. It doesn't make sense to me to talk about modifying headers after the page has loaded, which is in effect what you are asking.

我相信从客户端影响标头的唯一方法是当您使用 XmlHttpRequest 请求页面时。在页面加载后谈论修改标题对我来说没有意义,这实际上是您要问的。

回答by Joel Lundberg

What your C# example does, is add the following meta tag to the html page:

您的 C# 示例所做的是将以下元标记添加到 html 页面:

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

If you do this manually on the page that runs the JS code, it should work.

如果您在运行 JS 代码的页面上手动执行此操作,它应该可以工作。