即使iFrame的内容来自同一个来源,我如何阻止iFrame中的JavaScript访问外部网站的属性? [英] How can I prevent JavaScript in an iFrame to access properties of the outer site, even if the iFrame's content comes from the same origin?

查看:153
本文介绍了即使iFrame的内容来自同一个来源,我如何阻止iFrame中的JavaScript访问外部网站的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要一个iFrame,它总是限制它的内容,好像它来自不同的域,即使内容来自同一个来源。

Basically I want to have an iFrame which always restricts it's content as if it comes from a different domain, even if the content comes from the same origin.

是有没有办法做到这一点?

Is there any way to do this?

推荐答案

这将隐藏 window.parent 在子框架/窗口中,但不是顶部属性。

This will hide window.parent in the child frame/window, but not the top property.

window.parent 属性是STILL可访问的,直到子窗口/框架的onload事件结束。

BUT the window.parent property is STILL accessible till the end of the onload event of the child window/frame.

<html>
  <head>
    <style type="text/css">
      #wrapper {width:1000px;height:600px;}
    </style>
    <script type="text/javascript">
      window.onload = function() {
        var frm = document.getElementById('childFrame');
        var win = frm.contentWindow || (frm.contentDocument && frm.contentDocument.parentWindow) || (frm.document && frm.document.parentWindow);
        if (win) win.parent = null;
      }
    </script>
  </head>
  <body>
    <div id="wrapper">
      <iframe id="childFrame" src="child.html" frameborder="0" style="width:100%;height:100%;"></iframe>
    </div>
  </body>
</html>

这篇关于即使iFrame的内容来自同一个来源,我如何阻止iFrame中的JavaScript访问外部网站的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆