menteith menteith - 1 month ago 6
HTML Question

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

I have the following in HTML code:

<meta name="citation_journal_title" content="Psychological Bulletin" />


It is quite easy to get the content by using:

document.getElementsByName("citation_journal_title")[0].getAttribute("content")


However, I cannot deal with this:

<meta property="og:site_name" content="APA PsycNET" />


How do you retrieve the content of
og:site_name
?
I am aware of the question
How do I get the information from a meta tag with javascript?
but I'm looking for something quite simple like

document.getElementsByName("citation_journal_title")[0].getAttribute("content")

Answer

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" />