javascript 如何在javascript中添加元标记

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

How to add meta tag in javascript

javascriptmetatag

提问by Snehanjali Sahoo

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

我想<meta http-equiv="X-UA-Compatible" content="IE=edge">为特定页面添加。

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

但是我的页面是在一个HTML标签内呈现的。单击不同的模板时,只有内容会发生变化。所以我不能添加<meta>in<HEAD>部分。

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

有没有办法添加<meta http-equiv="X-UA-Compatible" content="IE=edge">using javascript

回答by T.J. Crowder

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

这里真正的答案必须是首先从服务器输出正确的标签。(遗憾的是,如果您需要支持 IE,您不能没有标签。:-|)

回答by Yussuf S

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

$('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.

如果要为页面描述添加元数据标签,请使用 DNN 页面的设置来添加描述和关键字。除此之外,修改 HEAD 的最佳方法是通过第三方模块将代码动态注入 HEAD。

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

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

可以将其他 HEAD 标签放入页面设置 > 高级设置 > 页眉标签。

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

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

回答by Paul Rad

Like this ?

像这样 ?

<script>
var meta = document.createElement('meta');
meta.setAttribute('http-equiv', 'X-UA-Compatible');
meta.setAttribute('content', 'IE=Edge');
document.getElementsByTagName('head')[0].appendChild(meta);
</script>