【发布时间】:2009-11-18 10:34:22
【问题描述】:
我的网站顶部有一个“工具栏”,页面内容是 iframe。 如何使用 javascript 找出 iframe 的当前 URL 是什么?
【问题讨论】:
标签: javascript iframe
我的网站顶部有一个“工具栏”,页面内容是 iframe。 如何使用 javascript 找出 iframe 的当前 URL 是什么?
【问题讨论】:
标签: javascript iframe
如果 iframe 位于不同的域中,或者违反了same origin policy,这可能是不可能的。例如,如果页面位于 example.com/foo,而 iframe 位于 example.org/bar,则无法获取位置。
如果你没有违反同源政策,你可以使用这样的:
window.frames["iframeID"].location.href
【讨论】:
检查“src”属性。
例如 window.frames["your_iframe"].src
【讨论】:
function getUrl()
{
var pageFrame = $find("frame_id_goes_here", document);
if(pageFrame) {
return pageFrame.location;
}
}
【讨论】:
window.frames[0].location.href 怎么样?
或者如果你知道 iframe 的 id,那么 document.getElementById("iframe1").src 应该可以工作。
【讨论】:
假设 iframe 是页面上的第一个:
document.getElementsByTagName('IFRAME')[0].contentDocument.location.href
当然,您总是可以通过 ID 获取它:
document.getElementById('myIframe').contentDocument.location.href
contentDocument 是关键。
祝你好运!
【讨论】: