23

I am a newbie, so excuse me if this is a silly question ..

So what I was trying is to get the title of a URL using JQuery/JS. I dont want to load the content of the url and then parse tags in it.

Let me be more clear, I have a set of urls, say 20 for which I want to display the titles .. the urls i am referring to are not the current urls, so I cant use js document.title ..

so i want to do something of the form SOMEFUNC.title(URL) and get its title. Is there any such function?

user1014390
  • 273
  • 1
  • 2
  • 5
  • What do you mean by `title of a URL`? The title of a HTML document stored at that location? – StuperUser Oct 26 '11 at 11:06
  • @StuperUser as i said, its not for the 'current' url or the page that is being visited now .. i have a set of urls .. for which i need the title .. lets say i have news.google.com, news.bbc.com, yahoo.co.uk, etc. .. now i want the titles of these pages .. – user1014390 Oct 26 '11 at 11:11
  • yes .. so for example for google.co.uk its should be Google - UK .. – user1014390 Oct 26 '11 at 11:13
  • 2
    The reason I make the clarification is because as you say: `i have a set of urls .. for which i need the title`; URLs don't have titles, HTML documents do. There's no library function to get the title of a document by URL, as you say, you'll have to get the documents then find the title of them. – StuperUser Oct 26 '11 at 11:24
  • 2
    Note though, from: http://api.jquery.com/jQuery.get/ `"Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol."` – StuperUser Oct 26 '11 at 11:25

3 Answers3

21

Something like this should work:

$.ajax({
  url: externalUrl,
  async: true,
  success: function(data) {
    var matches = data.match(/<title>(.*?)<\/title>/);
    alert(matches[0]);
  }   
});

TheSuperTramp is correct, above will not work if externalUrl is outside of your domain. Instead create this php file get_external_content.php:

<?php
function file_get_contents_curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$url = $_REQUEST["url"];
$html = file_get_contents_curl($url);

preg_match('/<title>(.+)<\/title>/',$html,$matches);
$title = $matches[1];

echo  json_encode(array("url" => $url, "title" => $title));

then in javascript:

function getTitle(externalUrl){
  var proxyurl = "http://localhost/get_external_content.php?url=" + externalUrl;
  $.ajax({
    url: proxyurl,
    async: true,
    success: function(response) {
      alert(response);
    },   
    error: function(e) {
      alert("error! " + e);
    }
  });
}
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
uncreative
  • 1,436
  • 9
  • 14
11

You can also get the title of any webpage using this API

http://textance.herokuapp.com/title/

$.ajax({
      url: "http://textance.herokuapp.com/title/www.bbc.co.uk",
      complete: function(data) {
        alert(data.responseText);
      }
});
Griwes
  • 8,805
  • 2
  • 43
  • 70
Durgesh
  • 406
  • 4
  • 8
1

Crossdomain request don't work with ajax, but what you could do is write a script on your server that fetch the title of a given site.

If you are using PHP you could use the file_get_contents and preg_match function to get the title. This guy here already provide the code for it.

http://www.cafewebmaster.com/php-get-page-title-function

Then in jQuery you could add this to an event or put it inside a function.

//For the purpose of this example let's use google
var url = "http://www.google.com";

$.ajax({
  type: "POST",
  url: "./getURLTitle.php",
  data: "{url: \"" + url + "\"}",
  success: function(data) {
     //do stuff here with the result
     alert(data);
  }   
});
Eric B.
  • 338
  • 3
  • 14