javascript get meta tag by name code example

Example 1: get meta tag value from url javascript

document.getElementsByTagName("meta").generator.getAttribute('version');

Example 2: js get meta content

//One liner

var meta_content = document.getElementById("meta_hint").getAttribute('content');

Example 3: js get meta-tag name

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