How to get the meta tag information from an external url?

I need to get the meta tag from a specific external url, but I found only examples using jquery to perform this functionality, and not pure javascript. In pure js, I found this post where it is carried out what I need, but not on any external page, but on the page itself.

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

Https://stackoverflow.com/questions/7524585/how-do-i-get-the-information-from-a-meta-tag-with-javascript

Does anyone know how I can get the information from meta tag of any url using pure javascript only?

Author: Comunidade, 2016-06-14

2 answers

Due to CORS restriction (Cross-Origin Resource Sharing, read about here), browsers block requests outside your domain by default.

But we can use a proxy like crossorigin.me to perform this type of request.

In the code below I perform a proxied request, I get the return that will be the HTML of the page, a function receives the return and does all necessary processing, I made a parse of the string for DOM elements, I used a piece of your question code to retrieve a specific tag and display it in a div.

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", 'https://crossorigin.me/http://example.com/');
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == XMLHttpRequest.DONE) {
    if (xmlhttp.status == 200) {
      tratativa(xmlhttp.responseText);
    } else {
      console.log('Error: ' + xmlhttp.statusText)
    }
  }
}
xmlhttp.send();

function tratativa(htmlString) {
  var parser = new DOMParser();
  var documentoBody = parser.parseFromString(htmlString, "text/html");

  var metaTags = documentoBody.getElementsByTagName('meta');

  for (var i = 0; i < metaTags.length; i++) {
    if (metaTags[i].getAttribute("name") == "viewport") {
      document.getElementById("retorno").innerHTML = metaTags[i].getAttribute("content");
    }
  }
}
<div id="retorno"></div>
 2
Author: Laerte, 2017-04-13 12:59:40

Can place the order normally as it does with any internal address. Then just create the crossdomain file.xml at the root of your site.

See Here a question about crossdomain.xml already solved.

For a full reference see the Adobe websitehttp://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html

 -1
Author: pho3nix, 2017-05-23 12:37:27