如何从带有JavaScript的元标记中获取信息?

时间:2021-08-17 06:20:03

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

我需要的信息是元标记。当property =“video”时,如何访问元标记的“内容”数据?

HTML:

HTML:

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

12 个解决方案

#1


80  

You can use this:

你可以用这个:

function getVideoContent() { 
   var metas = document.getElementsByTagName('meta'); 

   for (var i=0; i<metas.length; i++) { 
      if (metas[i].getAttribute("property") == "video") { 
         return metas[i].getAttribute("content"); 
      } 
   } 

    return "";
} 

#2


100  

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;

#3


34  

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

这里有很多难以理解的答案。这里有一个班轮

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

#4


13  

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

#5


11  

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

#6


11  

There is an easier way:

有一种更简单的方法:

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

#7


9  

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

#8


2  

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/muthupandiant/ogfLwdwt/

#9


2  

function getDescription() {
    var info = document.getElementsByTagName('meta');
    return [].filter.call(info, function (val) {
        if(val.name === 'description') return val;
    })[0].content;
}

update version:

更新版本:

function getDesc() {
    var desc = document.head.querySelector('meta[name=description]');
    return desc ? desc.content : undefined;
}

#10


1  

Way - [ 1 ]

方式 - [1]

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

Way - [ 2 ]

方式 - [2]

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

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

#11


0  

Here's a function that will return the content of any meta tag and will memoize the result, avoiding unnecessary querying of the DOM.

这是一个函数,它将返回任何元标记的内容并记住结果,避免不必要的查询DOM。

var getMetaContent = (function(){
        var metas = {};
        var metaGetter = function(metaName){
            var theMetaContent, wasDOMQueried = true;;
            if (metas[metaName]) {
                theMetaContent = metas[metaName];
                wasDOMQueried = false;
            }
            else {
                 Array.prototype.forEach.call(document.getElementsByTagName("meta"), function(el) {
                    if (el.name === metaName) theMetaContent = el.content;
                    metas[metaName] = theMetaContent;
                });
            }
            console.log("Q:wasDOMQueried? A:" + wasDOMQueried);
            return theMetaContent;
        }
        return metaGetter;
    })();

getMetaContent("description"); /* getMetaContent console.logs the content of the description metatag. If invoked a second time it confirms that the DOM  was only queried once */

And here's an extended version that also queries for open graph tags, and uses Array#some:

这是一个扩展版本,也查询开放图标签,并使用Array#some:

var getMetaContent = (function(){
        var metas = {};
        var metaGetter = function(metaName){
            wasDOMQueried = true;
            if (metas[metaName]) {
                wasDOMQueried = false;
            }
            else {
                 Array.prototype.some.call(document.getElementsByTagName("meta"), function(el) {
                        if(el.name === metaName){
                           metas[metaName] = el.content;
                           return true;
                        }
                        if(el.getAttribute("property") === metaName){
                           metas[metaName] = el.content;
                           return true;
                        }
                        else{
                          metas[metaName] = "meta tag not found";
                        }  
                    });
            }
            console.info("Q:wasDOMQueried? A:" + wasDOMQueried);
            console.info(metas);
            return metas[metaName];
        }
        return metaGetter;
    })();

getMetaContent("video"); // "http://video.com/video33353.mp4"

#12


0  

I personally prefer to just get them in one object hash, then I can access them anywhere. This could easily be set to an injectable variable and then everything could have it and it only grabbed once.

我个人更喜欢将它们放在一个对象哈希中,然后我可以在任何地方访问它们。这可以很容易地设置为一个注射变量,然后一切都可以拥有它,它只抓取一次。

By wrapping the function this can also be done as a one liner.

通过包裹功能,这也可以作为一个衬里来完成。

var meta = (function () {
    var m = document.querySelectorAll("meta"), r = {};
    for (var i = 0; i < m.length; i += 1) {
        r[m[i].getAttribute("name")] = m[i].getAttribute("content")
    }
    return r;
})();

#1


80  

You can use this:

你可以用这个:

function getVideoContent() { 
   var metas = document.getElementsByTagName('meta'); 

   for (var i=0; i<metas.length; i++) { 
      if (metas[i].getAttribute("property") == "video") { 
         return metas[i].getAttribute("content"); 
      } 
   } 

    return "";
} 

#2


100  

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;

#3


34  

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

这里有很多难以理解的答案。这里有一个班轮

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

#4


13  

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

#5


11  

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

#6


11  

There is an easier way:

有一种更简单的方法:

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

#7


9  

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

#8


2  

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/muthupandiant/ogfLwdwt/

#9


2  

function getDescription() {
    var info = document.getElementsByTagName('meta');
    return [].filter.call(info, function (val) {
        if(val.name === 'description') return val;
    })[0].content;
}

update version:

更新版本:

function getDesc() {
    var desc = document.head.querySelector('meta[name=description]');
    return desc ? desc.content : undefined;
}

#10


1  

Way - [ 1 ]

方式 - [1]

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

Way - [ 2 ]

方式 - [2]

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

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

#11


0  

Here's a function that will return the content of any meta tag and will memoize the result, avoiding unnecessary querying of the DOM.

这是一个函数,它将返回任何元标记的内容并记住结果,避免不必要的查询DOM。

var getMetaContent = (function(){
        var metas = {};
        var metaGetter = function(metaName){
            var theMetaContent, wasDOMQueried = true;;
            if (metas[metaName]) {
                theMetaContent = metas[metaName];
                wasDOMQueried = false;
            }
            else {
                 Array.prototype.forEach.call(document.getElementsByTagName("meta"), function(el) {
                    if (el.name === metaName) theMetaContent = el.content;
                    metas[metaName] = theMetaContent;
                });
            }
            console.log("Q:wasDOMQueried? A:" + wasDOMQueried);
            return theMetaContent;
        }
        return metaGetter;
    })();

getMetaContent("description"); /* getMetaContent console.logs the content of the description metatag. If invoked a second time it confirms that the DOM  was only queried once */

And here's an extended version that also queries for open graph tags, and uses Array#some:

这是一个扩展版本,也查询开放图标签,并使用Array#some:

var getMetaContent = (function(){
        var metas = {};
        var metaGetter = function(metaName){
            wasDOMQueried = true;
            if (metas[metaName]) {
                wasDOMQueried = false;
            }
            else {
                 Array.prototype.some.call(document.getElementsByTagName("meta"), function(el) {
                        if(el.name === metaName){
                           metas[metaName] = el.content;
                           return true;
                        }
                        if(el.getAttribute("property") === metaName){
                           metas[metaName] = el.content;
                           return true;
                        }
                        else{
                          metas[metaName] = "meta tag not found";
                        }  
                    });
            }
            console.info("Q:wasDOMQueried? A:" + wasDOMQueried);
            console.info(metas);
            return metas[metaName];
        }
        return metaGetter;
    })();

getMetaContent("video"); // "http://video.com/video33353.mp4"

#12


0  

I personally prefer to just get them in one object hash, then I can access them anywhere. This could easily be set to an injectable variable and then everything could have it and it only grabbed once.

我个人更喜欢将它们放在一个对象哈希中,然后我可以在任何地方访问它们。这可以很容易地设置为一个注射变量,然后一切都可以拥有它,它只抓取一次。

By wrapping the function this can also be done as a one liner.

通过包裹功能,这也可以作为一个衬里来完成。

var meta = (function () {
    var m = document.querySelectorAll("meta"), r = {};
    for (var i = 0; i < m.length; i += 1) {
        r[m[i].getAttribute("name")] = m[i].getAttribute("content")
    }
    return r;
})();