How to turn on IE9 Compatibility View programmatically in Javascript

21,346

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

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

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

Share:
21,346
Admin
Author by

Admin

Updated on March 22, 2020

Comments

  • Admin
    Admin about 4 years

    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

    • mplungjan
      mplungjan about 12 years
      Your reason for needing this is not very clear. Can you elaborate?
    • mplungjan
      mplungjan about 12 years
      document.documentMode is read-only msdn.microsoft.com/en-us/library/ie/cc196988%28v=vs.85%29.as‌​px and would need a reload anyway since it affects the rendering of the page
    • Strelok
      Strelok about 12 years
      But you're opening your own ASPX page. Why can't the page set the meta? I don't get the problem.