0

I'm trying to add a Open Graph tag to a website using the following JavaScript code, I need to add the open graph tags before the closing of the <head> but the code is not working and the open graph tags are not being added.

var newtext = document.createTextNode(" <meta property='og:type'   content='article' /> ");
var x=getElementsByTagName("head")[0]
x.appendChild(newtext);
4

2 回答 2

1

不要在 javascript 中添加元属性:此标记通常由不关心解释脚本的机器人或引擎解释。Open Grap 标签就是这种情况:它们必须静态添加。

于 2012-09-13T11:58:38.027 回答
0

使用var x=document.getElementsByTagName("head")[0],它应该可以工作。

工作代码:

var metaTag = document.createElement("meta");
metaTag.setAttribute("property", "og:type");
metaTag.setAttribute("content", "article");
var x=document.getElementsByTagName("head")[0];
x.appendChild(metaTag);
于 2012-09-13T11:59:04.390 回答