How to Get the Information from a Meta Tag With JavaScript

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

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

How do I retrieve the content of meta property og in JavaScript?

You need to use attribute selector [attr=value] to do this work. Use it in querySelector() like this

var attr = document.querySelector("meta[property='og:site_name']").getAttribute("content");console.log(attr);
<meta property="og:site_name" content="APA PsycNET" />

How to read meta data content via Javascript?

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

var value = document.querySelector('meta[itemprop="price"]').getAttribute("content");console.log(value)
<meta itemprop="price" content="175.03">

How to get the content of the content attribute for a meta tag with js/jquery

The text() method returns the text content within an element. This is not correct when trying to retrieve information from a meta element, as they have no text.

instead, to retrieve the value of the content attribute, use attr():

var content = $('meta[content]').attr('content');

How to get meta data?

You can use getAttribute() method in the specific element to retrieve values.

var meta = document.getElementsByTagName('meta');
console.log(meta[0].getAttribute("data-isunder13"));
<meta name="age" data-isunder13="some value">

How to get meta tag content values and average them

Yes, this is not too difficult using jQuery. This code will grab all the meta contents, convert them to integers, find the average rating, and then append some HTML at the bottom of your head element.

// first gather all the meta elements with an itemprop value of "ratingValue"
var metas = $('meta[itemprop="ratingValue"]').get();

// convert the content values of these elements to integers and put them in an array
var ratings = metas.map((m) => ~~m.content);

// calculate and round the average rating value
var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);

// create the HTML for the aggregateRating parent div
var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';

// create ratingValue meta HTML using the average rating
var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';

// create aggregateRating meta HTML using the rating count
var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';

// combine these strings and a closing tag, then append the HTML to the end of head
$('head').append(aggregateRating + ratingValue + reviewCount + '</div>');

or you could even use the Bernard method

$('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
['reviewCount', r.length + ' reviews']
].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));

Can't access data from meta tags

var metatags = document.getElementsByTagName('meta[id="candidate1"]'); this line is wrong. You should use querySelector to select by attribute.

document.write("<p>")document.write("Hello, world")document.write("</p>")
var metatags = document.querySelector('meta[id="candidate1"]');var candidate = metatags.getAttribute("data-candidate");document.write(metatags);document.write("<br /> Candidate -", candidate,"<hr />");

//If you want the values of both then use this codevar metatags = document.getElementsByTagName('meta');
Array.prototype.forEach.call(metatags, function(metaTag) { var candidate = metaTag.getAttribute("data-candidate"); document.write("<br /> Candidate -", candidate);});
Here is my code:
<body> <h1>Testing 1, 2</h1> <meta id="candidate1" data-candidate="1"> <meta id="candidate2" data-candidate="2"></body>

using javascript regex, get meta tags data from a web page

Here's how you do it by not using RegEx

No libraries, pure vanilla JS:

var meta = document.querySelectorAll('meta');
for(var i=0;i<meta.length;i++){
var content = meta[i].getAttribute('content'); /* here's the content */
}

http://jsfiddle.net/JA9Yq/

jQuery:

$('meta').each(function(index,tag){
var content = tag.getAttribute('content');
});

http://jsfiddle.net/duL6E/



Related Topics



Leave a reply



Submit