How to add meta tag in javascript

Snehanjali Sahoo picture Snehanjali Sahoo · Sep 24, 2013 · Viewed 120.9k times · Source

I want to add <meta http-equiv="X-UA-Compatible" content="IE=edge"> for a particular page.

But my pages are rendered inside one HTML tag. Only the content is changing on clicking different templates. So i cannot add the <meta> in <HEAD> section.

Is there any way to add the <meta http-equiv="X-UA-Compatible" content="IE=edge"> using javascript ?

Answer

T.J. Crowder picture T.J. Crowder · Sep 24, 2013

You can add it:

var meta = document.createElement('meta');
meta.httpEquiv = "X-UA-Compatible";
meta.content = "IE=edge";
document.getElementsByTagName('head')[0].appendChild(meta);

...but I wouldn't be surprised if by the time that ran, the browser had already made its decisions about how to render the page.

The real answer here has to be to output the correct tag from the server in the first place. (Sadly, you can't just not have the tag if you need to support IE. :-| )