views:

79

answers:

5

Hi. I have a 'toolbar' on the top of my website, and the content of the page is an iframe. How can I find out with javascript what the current URL of the iframe is?

A: 

How about window.frames[0].location.href ?

Or if you know the id of the iframe, then document.getElementById("iframe1").src should work.

Joy Dutta
A: 

Check the "src" attribute.

for example window.frames["your_iframe"].src

smercer
this returns the src location. what if the user navigates from the original page location. i need the actual location the page is at..!
rashcroft22
Yes, you'll want to check the location.href of the document, then.
smercer
A: 
function getUrl()
{
    var pageFrame = $find("frame_id_goes_here", document);
    if(pageFrame) {
        return pageFrame.location;
    }
}
A: 

Assuming the iframe is the first on the page:

document.getElementsByTagName('IFRAME')[0].contentDocument.location.href

Of course you could always just grab it by ID:

document.getElementById('myIframe').contentDocument.location.href

The contentDocument is the key.

Best of luck!

Funka
+1  A: 

This may not be possible if the iframe is in a different domain, or otherwise violates the same origin policy. For example, if the page is at example.com/foo and the iframe is at example.org/bar, you cannot get the location.

If you are not violating the same origin policy, you can use something like this:

window.frames["iframeID"].location.href
Buddy