Get metadata attribute in javascript

I'm having trouble getting information from the meta tag. I am trying to get img src from a website and can't figure it out. Here is an example of what I'm trying to do.

<meta property="og:image" content="http://foo.jpg"> var image = document.querySelector('meta[property="og:image"]').getAttribute('content'); 

I tried this, but it does not work. Any ideas?

+5
source share
1 answer
Items

meta are not special, you can request them and get your attributes in the usual way.

In this case, how do you get the value of the content attribute from the first meta[property="og:image"] element:

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

querySelector supported by all modern browsers, as well as IE8.

+19
source

Source: https://habr.com/ru/post/1215601/


All Articles