Fetch meta-tags from url only by jQuery

17,893

Solution 1

Use the html data retrieved from URL

$.get('http://www.guardian.co.uk/culture/2012/jun/21/jimmy-carr-apologises-error-tax', 
function(data) {
   $(data).find('meta[name=adescription]').attr("content");
});

Solution 2

It seems like this wouldn't be possible because of the Cross Origin Policy

There are definitely ways of doing it with combination client & server side changes. Instead I've been using this API in a project that works well as a simple REST API to get a url's open graph data. GET https://opengraph.io/api/1.0/site/<URL encoded site URL> https://opengraph.io/

It's been working for me as a client-side javascript only solution.

[NOTE: I have no relationship to this product or its creators. I found it through online research and used it in a project.]

Solution 3

It's possible using CORS Anywhere:

$.get("https://cors-anywhere.herokuapp.com/http://www.imdb.com/title/tt1375666/", function(data) {
    var meta = $(data).filter('meta[name="apple-itunes-app"]').attr("content");
    console.log(meta)
});

Used apple-itunes-app because it's an actual meta tag on IMDB.

Solution 4

With filter works fine!

You can try.

$.get('http://www.guardian.co.uk/culture/2012/jun/21/jimmy-carr-apologises-error-tax', function(data) {
    $(data).filter('meta[name=adescription]').attr("content");
});   
Share:
17,893
khex
Author by

khex

about me

Updated on June 18, 2022

Comments

  • khex
    khex almost 2 years

    Now a lot of scripts to facebook-style fetching data from url, but all of them work only in combination of jQuery and PHP. Is it possible to fetch url only by jQuery?

    I have found here how to get mata-tags of page by:

    $('meta[name=description]').attr("content");
    $("meta[property=og:title]").attr("content", document.title);
    

    But how correctly insert this query in jQuery.get() to get text values?

    $.get('http://www.imdb.com/title/tt1375666/', function(data) {
      $('meta[name=adescription]').attr("content");
    });
    

    And if the most popular sites use OpenGraph should I look in the direction of jQuery.getJSON()?

  • khex
    khex almost 12 years
    Thank you, but it returns me the whole page, and doesn't fide meta-tags in it
  • Rebecca
    Rebecca almost 8 years
    I just updated the post to explain - no relationship, just a user.
  • Martijn Pieters
    Martijn Pieters almost 8 years
    Thanks, much appreciated!