考虑以下三页。
Foo.html
在浏览器中本地打开,因此具有前缀为file:///
的 URL。Bar.html
与Foo.html
位于同一目录中。Bar2.html
位于/var/www
中,我在localhost
上运行Apache
。<
Foo.html
<html>
<head>
<script>
foo = function() {
alert("frame changed");
};
</script>
</head>
<body>
<iframe width="200" height="300" src="Bar.html" id="my-iframe" onLoad="foo" />
</body>
</html>
Bar.html
<html>
<body>
<iframe width="200" height="300" src="http://localhost/Bar2.html" id="my-iframe" />
</body>
</html>
Bar2.html
<html>
<head>
<script>
if (top.location != self.location){
parent.location = self.location;
}
</script>
</head>
<body>
<button type="button" onclick="document.location.href='http://bing.com'">Hello World</button>
</body>
</html>
当 Foo.html
在 Firefox 中加载时,通过在命令行上运行 firefox/path/to/Foo.html
, 中的框架破坏代码Bar2.html
脱离了 Bar.html
。此时,用户会收到框架已更改
的警报。
当我单击按钮时,iframe
发生变化(按钮消失),但我没有收到警报。
为什么页面更改时 onLoad
没有第二次触发?
最佳答案
问题在于,当 Bar2.html 被 Bar.html 中的 iframe 加载时,此条件为 false
if (top.location != self.location)
因此,当 Bar.html 加载 Bar2.html 时,这被调用
parent.location = self.location;
这会在页面中发出重定向并取消 onload 事件的影响,因为从技术上讲,页面从未完成加载。
关于javascript - 为什么 `onload` 事件不会在具有 Framebusting 的嵌套 iframe 上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483193/