How do I get the information from a meta tag with JavaScript?
The information I need is in a meta tag. How can I access the "content"
data of the meta tag when property="video"
?
HTML:
<meta property="video" content="http://video.com/video33353.mp4" />
The other answers should probably do the trick, but this one is simpler and does not require jQuery:
document.head.querySelector("[property~=video][content]").content;
The original question used an RDFa tag with a property=""
attribute. For the normal HTML <meta name="" …>
tags you could use something like:
document.querySelector('meta[name="description"]').content
You can use this:
function getMeta(metaName) {
const metas = document.getElementsByTagName('meta');
for (let i = 0; i < metas.length; i++) {
if (metas[i].getAttribute('name') === metaName) {
return metas[i].getAttribute('content');
}
}
return '';
}
console.log(getMeta('video'));
One liner here
document.querySelector("meta[property='og:image']").getAttribute("content");
There is an easier way:
document.getElementsByName('name of metatag')[0].getAttribute('content')
function getMetaContentByName(name,content){
var content = (content==null)?'content':content;
return document.querySelector("meta[name='"+name+"']").getAttribute(content);
}
Used in this way:
getMetaContentByName("video");
The example on this page:
getMetaContentByName("twitter:domain");