How to turn on IE9 Compatibility View programmatically in Javascript

依然范特西╮ 提交于 2020-01-05 09:04:48

问题


I need to turn on IE compatibility programmatically.

I know this works in 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:

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?

Thanks in advance


回答1:


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

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.

For example, for IIS7/IIS7.5, you can use 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.




回答2:


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

<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.




回答3:


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

<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.

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.



来源:https://stackoverflow.com/questions/9819026/how-to-turn-on-ie9-compatibility-view-programmatically-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!