javascript - 为什么 `onload` 事件不会在具有 Framebusting 的嵌套 iframe 上触发?

标签 javascript html security iframe framebusting

考虑以下三页。

  1. Foo.html 在浏览器中本地打开,因此具有前缀为 file:/// 的 URL。
  2. Bar.htmlFoo.html 位于同一目录中。
  3. 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/

相关文章:

javascript - Onclick - 向下微调 div

javascript - 避免在 foreach 循环中出现 "Unresponsive Script"消息

javascript - 主干 - 管理应用程序 View

javascript - JQuery - 仅使用选择器切换 div

javascript - 在 html5 Canvas 上绘制多个粒子元素而无需渲染终止

css - 通过 CSS 在图像上水平居中文本

css - 将 DIV 跨越多行

iphone - 使用Apple80211 api时如何知道OPEN、WPA、WPA2、WEP等安全类型?

php - 在数据到达服务器之前加密数据

从数据库读取时PHP清理字符串