Topic: get the title of a remote webpage

Hi all,
I have two textbox like that:

<p><label for="bookmark_address">Address</label><br/>
<%= text_field 'bookmark', 'address' , :size => 120 %></p>
<p><label for="bookmark_address">Title</label><br/>
<%= text_field 'bookmark', 'title' , :size => 120 %></p>

When the first box is filled with an URL eg. , I want the second one to display the title of that page eg. "Rails Forum / Ruby on Rails Help and Discussion Forum".

I know I need to use getElementById("title") to get the title of a page but how can I do it with a remote page, do I need to use XMLHttpRequest?

thanx in advance


Re: get the title of a remote webpage

I doubt you'll even be able to use XMLHttpRequest. I believe XHR requests have to be done within the same domain.

vinnie - rails forum admin

Re: get the title of a remote webpage

You can do this with Ruby if you don't mind making two requests (one to your server, then one to the entered URL). Check out this great article on Screen Scraping for examples of grabbing the contents of another page and parsing it.

You can then combine this with observe_field and a little RJS to set the value of the text field. I can probably post an example if you need it.

One thing to watch out for, and I'm not certain how you plan to handle this, is if the URL is in the middle of being typed and is only partially there, you don't want to try to fetch a site that doesn't exist, so perhaps you should only send the request when the field loses focus or something.

Railscasts - Free Ruby on Rails Screencasts