0

I'm trying to extract the page title of an external site by using a url.

You know how "document.title" returns the title of the page the JS is running on? I was wondering if I could say "'http://google.com'.title" (doesn't work) or something similar to get the title of another page.

UPDATE: I did some searching and apparently this can be done with JQuery. see http://www.google.com/search?q=jquery+extract+page+title. and if I understand correctly JQuery is kind of an extension of javascript and its supported in what I'm doing. so can someone post the JQuery code that can accomplish this?

this is the basic idea: there is an input box labled "url". And a button labled "Convert to page title" and the intention is that the value of the input box will change to the Title of the url given by the user (after they click on the button)

function getTitle(url) {

    var title = [CODE HERE];
    return title;

}

thank you!

  • I took your question wrong.. I only read title... this link will help you somewhere... http://stackoverflow.com/questions/7126549/reading-text-file-from-external-folder-via-xml-path-name-google-maps-api – Fahim Parkar Feb 05 '12 at 06:53
  • thanks fahim. I saw an idea in a question linked to from there that may help. see below – Anthony Newman Feb 05 '12 at 08:13

2 Answers2

1

That is not possible through Javascript. You must use a server script to achieve this.

Ananth
  • 4,227
  • 2
  • 20
  • 26
  • then can JQuery do it? I just looked at [this question](http://stackoverflow.com/questions/1981815/jquery-read-a-text-file) (see the first answer by T. Stone) and it seems to be along the lines of what I'm looking for. However, I don't know JQuery and the code looks like it finds the H2 tags? Do I simply need to change `h2` to `title`? – Anthony Newman Feb 05 '12 at 08:05
  • Also the OP (at the question I linked to just above) said that the file was too big and someone suggested "loading it in pieces with a series (or loop)". Can someone also help me out with that? I'm not sure how you'd break it up with a loop. ||| and one more question: does the JQuery code in that question work for external sites (sites I don't own, like http://www.google.com)? – Anthony Newman Feb 05 '12 at 08:12
  • @AnthonyNewman you can not get the title of the page from another domain even with the help of jQuery. You can do it only requesting that page with the help of the server-side script. – Cheery Feb 05 '12 at 08:31
  • @AnthonyNewman You can do it with jQuery using the method below, but the answer by Ananth is not incorrect. The answer i gave below makes a call to YQL, which then uses a server call to pull cross domain content. I have no idea how fast or reliable YQL is... – Mikey G Feb 05 '12 at 09:00
0

I think you're looking for $.ajax, since this is a cross domain request, which can be done from js using a call to YQL:

http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide

EDIT

Okay I finally got a working example: http://jsfiddle.net/zXVcy/

EDIT

Here is how to fix the script tag problem in xdomain.js:

   if (_success) {
    var dataString = "";
    try { 
    dataString = data.results[0].replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '');
    }
    catch {
    dataString = data.results[0];

}
    // Fake XHR callback.
    _success.call(this, {
    responseText: dataString
    }, 'success');
       }
    }
Mikey G
  • 3,473
  • 1
  • 22
  • 27
  • If you plan on using this, you'll need to get the xdomainajax.js file. I wouldn't rely on the reference in the fiddle for actual code. – Mikey G Feb 05 '12 at 09:02
  • Okay Mikey. I successfully adapted it into my code, but there's one minor problem I hope you can shed some light on. Here is the basic idea of my code: http://jsfiddle.net/C47Gn/1/. If you put http://www.google.com or any other url into the input box and click "Convert", it works. The one domain that doesn't work is http://www.nytimes.com. Try putting http://www.nytimes.com/2012/02/06/sports/football/super-bowl-resilient-giants-edge-patriots-to-win-super-bowl-xlvi.html into the box and it doesn't work. What's the problem here? – Anthony Newman Feb 06 '12 at 08:14
  • I found the problem. It looks like the xdomain script removes – Mikey G Feb 06 '12 at 15:01
  • @AnthonyNewman I added an example to my answer above. I'm not sure if you'll need to add an exception to the catch or not, i haven't done it very often with js. – Mikey G Feb 06 '12 at 15:03
  • ok good point. I tried adding the try-catch you suggested but it doesn't work. See http://jsfiddle.net/C47Gn/3/ (I added the try-catch to http://yourjavascript.com/1162213222/jquery.xdomainajax.js, which I uploaded) – Anthony Newman Feb 06 '12 at 19:29
  • @AnthonyNewman sorry that was never going to work... I didn't look at it in context at all. I think the above will work now. – Mikey G Feb 06 '12 at 22:27
  • no it's ok. it's just that I'm a noob and don't know JS well. So I tried your suggestion; see http://jsfiddle.net/C47Gn/6/, but there's an error: res.responseText is undefined Line: 39 – Anthony Newman Feb 06 '12 at 22:54
  • @AnthonyNewman hmmm... okay, so it looks like the replace was blowing up because there was no response from the page. I think there must be some kind of a redirect on that page. I'll have to look at this some more later. I'll let you know if I figure anything out. – Mikey G Feb 07 '12 at 00:40
  • Yea it looks like the problem is that the page is returning a 301 - Permanently moved... So either you'll have to use the final url if you know it, or you'll have to handle it in the response. I'll try to put something in my answer for you if I have time. – Mikey G Feb 07 '12 at 01:40
  • @AnthonyNewman If you are dealing primarily with the NYTimes, you might want to try their API http://developer.nytimes.com/docs It will most likely be more dependable than YQL anyway, and you probably can get the exact information you want, which should make it faster too. – Mikey G Feb 07 '12 at 01:48