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
2 Answers
Reset to default 10You 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
版权声明:本文标题:jquery - How do I retrieve the content of meta property og in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741628112a2389198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论