5

How I can read the title of a remote web page with javascript? Suppose the web page is:

www.google.com

I want to read the title of that page; how should I do this?

6
  • You want to find the title of the current page, or of a page at some remote domain?
    – Sampson
    May 29, 2012 at 5:40
  • "pick" is totally the wrong word here. May 29, 2012 at 5:40
  • No i want to get the title of a remote page
    – h_a86
    May 29, 2012 at 5:41
  • @Ajay The question is not a basic as it looks. But it would still have been possible to find the answer via searching..
    – nfechner
    May 29, 2012 at 5:45
  • @PriyankPatel jQuery ain't gonna help; it's just not possible (unless aided by server-side)
    – Ja͢ck
    May 29, 2012 at 5:51

4 Answers 4

4

You won't be able to get this data with jQuery alone, however you can use jQuery to communicate with PHP, or some other server-side language that can do the heavy lifting for you. For instance, suppose we have the following in a PHP script on our server:

<?php # getTitle.php

    if ( $_POST["url"] ) {
        $doc = new DOMDocument();
        @$doc->loadHTML( file_get_contents( $_POST["url"] ) );
        $xpt = new DOMXPath( $doc );
        $output = $xpt->query("//title")->item(0)->nodeValue;
    } else {
        $output = "URL not provided";
    }

    echo $output;

?>

With this, we could have the following jQuery:

$.post("getTitle.php", { url:'http://example.com' }, function( data ) {
    alert(data);
});
0
2

Getting the content of a remote page you have no control over is going to be a problem because of the same-origin-policy. For more information look here: How to get the content of a remote page with JavaScript?

-1

Try this

alert(document.title);

in your case i guess you would only be using the document.title

1
  • 4
    The poster wants the title of a remote page.
    – nfechner
    May 29, 2012 at 5:43
-1

The effective method is to write some Server side code (using PHP/ASP/.NET) and pass the URL via AJAX in script and get the title of any remote page.

Not the answer you're looking for? Browse other questions tagged or ask your own question.