Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the information from a meta tag with JavaScript?

People also ask

How do I read meta tags?

If you want to find out whether a given page is using meta tags, just right-click anywhere on the page and select “View Page Source.” A new tab will open in Chrome (in Firefox, it'll be a pop-up window). The part at the top, or “head” of the page, is where the meta tags would be.

What is meta data in Javascript?

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. Metadata will not be displayed on the page, but is machine parsable.

How do I extract meta data from a website?

In Firefox, Chrome, Internet Explorer or Opera, right click anywhere on the page and select View Source or simply Source. You will be presented with a window full of code, which you'll need to search for the ID. Do this with Edit > Find or the Windows shortcut CTRL-F (Command ⌘ + F on Mac).


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

If you use JQuery, you can use:

$("meta[property='video']").attr('content');