【问题标题】:changing iframe url changes parent window url Blocked a frame with origin更改 iframe url 更改父窗口 url 阻止具有原点的框架
【发布时间】:2019-01-28 11:10:31
【问题描述】:

我用这个代码

<script>
  <!-- Hide Script
    if (top.location != self.location) {
     top.location = self.location
    }
  //End Hide Script -->
</script>

但最近我在 chrome 控制台中遇到此类错误,并且代码停止工作

(index):15 不安全的 JavaScript 尝试从 URL 为“http://site.test”的框架中为来源为“http://preview.themeforest.net”的框架启动导航。尝试导航的框架以它的顶级窗口为目标,但它既不与它的目标同源,也没有收到用户手势。见https://www.chromestatus.com/features/5851021045661696

(index):21 Uncaught DOMException: Failed to execute 'replace' on 'Location': 当前窗口无权将目标框架导航到'http://site.test''。

在 Firefox 和 Opera 中,此代码有效。在 Chrome 中它不起作用。

如何修复这些错误?

有点像this question

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    请查看同源政策: https://en.wikipedia.org/wiki/Same-origin_policy

    该错误的原因是您违反了该政策。

    我知道避免这种情况的唯一方法是使用 allow-top-navigation 标志设置 iframe 沙盒(有点受限)。

    <iframe src="demo_iframe_sandbox.htm" sandbox="allow-top-navigation"></iframe>
    

    来源:https://www.w3schools.com/tags/att_iframe_sandbox.asp

    检查限制,看看该解决方案是否适合您。

    【讨论】:

    • 我可以在 site.test 这个子 iframe 沙箱中添加它吗?通过 js 这里 iframe 代码
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 2010-12-24
    • 1970-01-01
    • 2012-05-08
    • 2012-06-27
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多