Some websites have code to “break out” of IFRAME
enclosures, meaning that if a page A
is loaded as an IFRAME
inside an parent page P
A
中的一些 Javascript 将外部窗口重定向到 A
。
通常这个 Javascript 看起来像这样:
<script type="text/javascript">
if (top.location.href != self.location.href)
top.location.href = self.location.href;
</script>
我的问题是:作为父页面 P
的作者而不是内页 A
的作者,我该如何防止 A
-出去?
PS 在我看来,这应该是跨站点安全违规,但事实并非如此。
原文由 Jason Cohen 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试使用 onbeforeunload 属性,它可以让用户选择是否要离开页面。
示例: https ://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload
在 HTML5 中,您可以使用沙盒属性。请参阅下面的 Pankrat 的回答。 http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/