Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add meta tag in JavaScript

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 ?

like image 547
Snehanjali Sahoo Avatar asked Sep 24 '13 12:09

Snehanjali Sahoo


People also ask

How do you add meta tags to a script?

You can add it: var meta = document. createElement('meta'); meta. httpEquiv = "X-UA-Compatible"; meta.

What is meta tag in JavaScript?

The <meta> tag defines metadata about an HTML document. Metadata is data (information) about data. <meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings.

How do I change meta tags in JavaScript?

To use JavaScript to change the meta tags of the page, we can set the properties that are available in a meta element. and set its content attribute value. and set its content attribute value.


2 Answers

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

like image 172
T.J. Crowder Avatar answered Sep 21 '22 01:09

T.J. Crowder


$('head').append('<meta http-equiv="X-UA-Compatible" content="IE=Edge" />'); 

or

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

Though I'm not certain it will have an affect as it will be generated after the page is loaded

If you want to add meta data tags for page description, use the SETTINGS of your DNN page to add Description and Keywords. Beyond that, the best way to go when modifying the HEAD is to dynamically inject your code into the HEAD via a third party module.

Found at http://www.dotnetnuke.com/Resources/Forums/forumid/7/threadid/298385/scope/posts.aspx

This may allow other meta tags, if you're lucky

Additional HEAD tags can be placed into Page Settings > Advanced Settings > Page Header Tags.

Found at http://www.dotnetnuke.com/Resources/Forums/forumid/-1/postid/223250/scope/posts.aspx

like image 26
Yussuf S Avatar answered Sep 20 '22 01:09

Yussuf S