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?
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
Check the "src" attribute.
for example window.frames["your_iframe"].src
function getUrl()
{
var pageFrame = $find("frame_id_goes_here", document);
if(pageFrame) {
return pageFrame.location;
}
}
How about window.frames[0].location.href
?
Or if you know the id
of the iframe, then document.getElementById("iframe1").src
should work.
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!
精彩评论