How to pull the title and url from an iframe?


#1

I have an iframe in which the user can browse. When they hit the ‘add
this page’ button i need to retrieve the url and the title of the page
currently shown in the iframe.

Does anyone know how to do this? Do i need to assign a variable name to
my iframe? (currently it just sits in my template as a bit of html)


#2

we need to pull the iframe into a js function, and from there, grabbing
the url + the title should be a flip. untested code, but it should be
something like the below, gathering the iframe’s attributes are

top.iframe_name.location.href

should give u “iframe_url” and

top.iframe_name.document.title

should give u the iframe’s

hope this helps

shai

Thanks for the advice shai but this just isn’t working for me and i
don’t know why. I installed firebug for firefox, which lists javascript
errors (among other things) and it’s complaining

“top.myframe has no properties” (myframe is the name of my iframe)

I must admit i don’t even know how to take this value and do something
with it in rails, so at the minute to test it i’m just doing an alert
box with the returned string. But it seems to be failing while
accessing the myframe object.

Do you have any idea what the problem might be? Thanks again for
helping by the way.


#3

Max W. wrote:

I have an iframe in which the user can browse. When they hit the ‘add
this page’ button i need to retrieve the url and the title of the page
currently shown in the iframe.

Does anyone know how to do this? Do i need to assign a variable name to
my iframe? (currently it just sits in my template as a bit of html)

doing this via javascript is probably the best solution. check out the
following link if the explanation below is vague:
http://www.quirksmode.org/js/frameintro.html

we need to pull the iframe into a js function, and from there, grabbing
the url + the title should be a flip. untested code, but it should be
something like the below, gathering the iframe’s attributes are

top.iframe_name.location.href

should give u “iframe_url” and

top.iframe_name.document.title

should give u the iframe’s

hope this helps

shai


#4

I think the “top” only exists in the iframe. Your javascript is in
there, right? There’s probably another way to get the information from
in the iframe on javascript executing in the full window.

On Jul 24, 10:15 am, Max W. removed_email_address@domain.invalid


#5

“top.myframe has no properties” (myframe is the name of my iframe)

I must admit i don’t even know how to take this value and do something
with it in rails, so at the minute to test it i’m just doing an alert
box with the returned string. But it seems to be failing while
accessing the myframe object.

if the ‘add this page’ button is in the iframe, i believe you can use
top.methods etc … if the button is in the main page, and you want to
access the iframe object, you can use:

document.frames.myframe # this returns the iframe object

i believe you’ll need to adjust the syntax around a little bit for
browser compatibility - ie, firefox, safari, and others use slightly
different syntaxes, but they all are similar, give or take … you might
have to google around for this if things aren’t working - javascript
syntax differences may be one of the problems (if you see there are
differences per browser).