更改 iframe 网址会更改父窗口网址 阻止具有原点的框架



我使用此代码

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

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

(索引(:15 不安全的 JavaScript 尝试从 URL 为"http://site.test"的框架启动对源"http://preview.themeforest.net"的框架的导航。尝试导航的框架以顶级窗口为目标,但与其目标既不同源,也没有收到用户手势。请参阅 https://www.chromestatus.com/features/5851021045661696。

(索引(:21 未捕获的 DOMException:无法在"位置"上执行"替换":当前窗口无权将目标帧导航到"http://site.test"。

在Firefox和Opera中,这段代码是有效的。在Chrome中它不起作用。

如何修复这些错误?

有点像这个问题

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

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

我知道避免这种情况的唯一方法是使用允许顶部导航标志设置 iframe 沙盒(有点受限(。

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

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

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

最新更新