如何使用 JavaScript 从元标记中获取信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7524585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:35:50  来源:igfitidea点击:

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

javascripthtmlgreasemonkeymeta-tags

提问by supercoolville

The information I need is in a meta tag. How can I access the "content"data of the meta tag when property="video"?

我需要的信息在元标记中。我怎样才能在什么时候访问"content"元标记的数据property="video"

HTML:

HTML:

<meta property="video" content="http://video.com/video33353.mp4" />

采纳答案by Saket

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

回答by joepio

The other answers should probably do the trick, but this one is simpler and does not require jQuery:

其他答案应该可以解决问题,但这个答案更简单,不需要 jQuery:

document.head.querySelector("[property~=video][content]").content;

The original question used an RDFatag with a property=""attribute. For the normal HTML <meta name="" …>tags you could use something like:

原始问题使用带有属性的RDFa标记property=""。对于普通的 HTML<meta name="" …>标签,您可以使用以下内容:

document.querySelector('meta[name="description"]').content

回答by Ced

A lot of hard to read answer here. One liner here

这里有很多难以阅读的答案。一个班轮在这里

document.querySelector("meta[property='og:image']").getAttribute("content");

回答by muchacho

There is an easier way:

有一个更简单的方法:

document.getElementsByName('name of metatag')[0].getAttribute('content')

回答by devMariusz

function getMetaContentByName(name,content){
   var content = (content==null)?'content':content;
   return document.querySelector("meta[name='"+name+"']").getAttribute(content);
}

Used in this way:

以这种方式使用:

getMetaContentByName("video");

The example on this page:

此页面上的示例:

getMetaContentByName("twitter:domain");

回答by Elise Chant

If you use JQuery, you can use:

如果您使用 JQuery,您可以使用:

$("meta[property='video']").attr('content');

回答by Prameet Jain

In Jquery you can achieve this with:

在 Jquery 中,您可以通过以下方式实现:

$("meta[property='video']");

In JavaScript you can achieve this with:

在 JavaScript 中,您可以通过以下方式实现:

document.getElementsByTagName('meta').item(property='video');

回答by Юрий Светлов

Way- [1 ]

方式- [1 ]

function getMetaContent(property, name){
    return document.head.querySelector("["+property+"="+name+"]").content;
}
console.log(getMetaContent('name', 'csrf-token'));

You may get error: Uncaught TypeError: Cannot read property 'getAttribute' of null

您可能会收到错误消息:Uncaught TypeError: Cannot read property 'getAttribute' of null



Way- [2 ]

方式- [2 ]

function getMetaContent(name){
    return document.getElementsByTagName('meta')[name].getAttribute("content");
}
console.log(getMetaContent('csrf-token'));

You may get error: Uncaught TypeError: Cannot read property 'getAttribute' of null

您可能会收到错误消息:Uncaught TypeError: Cannot read property 'getAttribute' of null



Way- [3 ]

方式- [3 ]

function getMetaContent(name){
    name = document.getElementsByTagName('meta')[name];
    if(name != undefined){
        name = name.getAttribute("content");
        if(name != undefined){
            return name;
        }
    }
    return null;
}
console.log(getMetaContent('csrf-token'));

Instead getting error, you get null, that is good.

而不是得到错误,你得到null,那很好。

回答by Erik Campobadal

Simple one, right?

很简单吧?

document.head.querySelector("meta[property=video]").content

回答by muTheTechie

This code works for me

这段代码对我有用

<meta name="text" property="text" content="This is text" />
<meta name="video" property="text" content="http://video.com/video33353.mp4" />

JS

JS

var x = document.getElementsByTagName("META");
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        if (x[i].name=="video")
        {
             alert(x[i].content);
         }

    }    

Example fiddle: http://jsfiddle.net/muthupandiant/ogfLwdwt/

小提琴示例:http: //jsfiddle.net/muthupdiant/ogfLwdwt/