admin管理员组

文章数量:1295855

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

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")
Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Oct 9, 2016 at 17:39 menteithmenteith 67817 silver badges56 bronze badges 2
  • Possible duplicate of Find an element in DOM based on an attribute value – chiliNUT Commented Oct 9, 2016 at 17:59
  • stackoverflow./a/78907991/9303782 – Hein Soe Commented Sep 10, 2024 at 10:02
Add a ment  | 

2 Answers 2

Reset to default 10

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

In Jquery, you can use attr()

$('meta[property="og:site_name"]').attr('content')

In JavaScript, you can use querySelector

querySelector is supported by all modern browsers, and also IE8.

var element = document.querySelector('meta[property="og:site_name"]');
var content = element && element.getAttribute("content");
console.log(content);

References

  • How querySelector works?
  • w3schools article for querySelector
  • Attribute Contains Selector

console.log($('meta[property="og:site_name"]').attr('content'));

var element = document.querySelector('meta[property="og:site_name"]');
var content = element && element.getAttribute("content");
console.log(content);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta property="og:site_name" content="APA PsycNET" />

本文标签: jqueryHow do I retrieve the content of meta property og in JavaScriptStack Overflow