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
2009-11-18 00:50:24
A:
Check the "src" attribute.
for example window.frames["your_iframe"].src
smercer
2009-11-18 00:50:59
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
2009-11-18 01:16:39
Yes, you'll want to check the location.href of the document, then.
smercer
2009-11-18 02:57:48
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
2009-11-18 02:10:16
+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
2009-11-18 02:34:22