0

I need to do what a bookmarklet does but from my page directly.

I need to pull the document.title property of a web page given that url.

So say a user types in www.google.com, I want to be able to somehow pull up google.com maybe in an iframe and than access the document.title property.

I know that bookmarklets ( the javacript that runs from bookmark bar ) can access the document.title property of any site the user is on and then ajax that information to the server.

This is essentially what I want to do but from my web page directly with out the use of a bookmarklet.

CS_2013
  • 1,158
  • 3
  • 13
  • 24
  • Possible duplicate of [How can I get the title of a webpage given the url (an external url) using JQuery/JS](http://stackoverflow.com/questions/7901760/how-can-i-get-the-title-of-a-webpage-given-the-url-an-external-url-using-jquer) – Elliot Bonneville May 16 '12 at 19:22
  • i was hoping there would be a "hack around"...bookmarklets can run on any page....which makes them powerful in a way...but for that power to run on any site...the user has to drag drop it into the bookmarks...if only there was a hack to emulate this. – CS_2013 May 16 '12 at 19:26

3 Answers3

2

According to This question You can achive this using PHP, try this code :

    <?php

function getTitle($Url){
    $str = file_get_contents($Url);
    if(strlen($str)>0){
        preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
        return $title[1];
    }
}
//Example:
echo getTitle("http://www.washingtontimes.com/");

?>

However, i assume it is possible to read file content with JS and do the same logic of searching for the tags.

Try searching here

Community
  • 1
  • 1
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
1

Unfortunately, its not that easy. For security reasons, JavaScript is not allowed to access the document object of a frame or window that is not on the same domain. This sort of thing has to be done with a request to a backend PHP script that can fetch the requested page, go through the DOM, and retrieve the text in the <title> tag. If you don't have that capability, what you're asking will be much harder.

Here is the basic PHP script, which will fetch the page and use PHP's DOM extension to parse the page's title:

<?php
$html = file_get_contents($_GET["url"]);

$dom = new DOMDocument;
$dom->loadXML($html);
$titles = $dom->getElementsByTagName('title');

foreach ($titles as $title) {
    echo $title->nodeValue;
}
?>

Demo: http://www.dstrout.net/pub/title.htm

Ashley Strout
  • 6,107
  • 5
  • 25
  • 45
  • i was hoping there would be a "hack around"...bookmarklets can run on any page....which makes them powerful in a way...but for that power to run on any site...the user has to drag drop it into the bookmarks...if only there was a hack to emulate this. – CS_2013 May 16 '12 at 19:25
0

You could write a server side script that would retrieve the page for you (i.e. using curl) and pars the dom and return the desired properties as json. Then call it with ajax.

Layton Everson
  • 1,148
  • 9
  • 18