【发布时间】:2015-09-10 12:27:15
【问题描述】:
我需要获取页面上 iframe 所在的页面 url,因为我们使用 iframe 来处理订购产品。但是,对于我的生活,我似乎无法让它显示 iframe 的 URL(显示是一个测试,因为当我看到它有效时它会自动发送电子邮件)
代码:
<button onClick="myFunction()">Test</button>
<script>
function myFunction() {
alert(document.getElementById("iFrame").documentWindow.location.href);
}
</script>
<iframe src="https://iframe.mydomain.co.uk" id="iFrame" width="98%" height="100%"></iframe>
此代码正在https://products.mydomain.co.uk 上使用。 正如我所说,警报只是在那里,所以我只需按下按钮即可看到它显示新域,以便我可以执行其余部分所需的操作。
有什么想法吗?
【问题讨论】:
-
尝试 document.getElementById("iframe_id").contentWindow.location.href
-
不,这也不行:/
-
iframe 和主页是否在同一个域中?
-
主域相同,子域不同
标签: javascript html iframe