场景如下:
该页面中有一个页面和一个 iframe。首先,它们都来自同一域(来源)。 iframe 可以包含指向其他域的链接(或更改 window.location.href 的 javascript)。我想在外框架中进行检测,当用户导航离开 iframe 中的原始域时,将外页面的 window.location.href 替换为用户导航到的 URL,从而结束应用程序在外框架中运行。
尝试使用beforeunload事件,但内部框架的location.href没有改变。此外,iframe DOM 元素的 src 属性在其 contentDocument 更改时不会更改。
我的问题在当前的浏览器实现中可以解决吗?或者您将使用什么方法来获得最接近所描述的行为?
最佳答案
$(document).ready(function () {
$("#widget").load(function () {
var ifr = document.getElementById("widget")
var anchors = ifr.contentDocument.getElementsByTagName("a");
for (var i in anchors) {
anchors[i].setAttribute("onClick", "testUrl()");
}
});
});
function testUrl() {
//your code goes here for validating the URLs...
}
关于javascript - 如何检测用户从外部窗口在 iframe 中导航到的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182891/