rksh
rksh

Reputation: 4050

Javascript getElementsByTagName not working?

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);

Upvotes: 0

Views: 149

Answers (2)

Krishna Kumar
Krishna Kumar

Reputation: 2131

Use var x=document.getElementsByTagName("head")[0], and it should work.

Working code:

var metaTag = document.createElement("meta");
metaTag.setAttribute("property", "og:type");
metaTag.setAttribute("content", "article");
var x=document.getElementsByTagName("head")[0];
x.appendChild(metaTag);

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382514

Don't add a meta property in javascript : this tag is usually interpreted by bot or engines who don't care about interpreting your scripts. That's the case for the Open Grap tags : they must be added statically.

Upvotes: 1

Related Questions