【发布时间】:2021-03-02 06:30:48
【问题描述】:
我有一个这样定义的沙盒 iframe:
<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals" src="...">
</iframe>
当我在 iframe 中设置 document.domain 时,我收到以下错误:
Uncaught DOMException: Failed to set the 'domain' property on 'Document': Assignment is forbidden for sandboxed iframes.
如何允许在 iframe 中设置 document.domain?
【问题讨论】:
-
不要
sandboxiframe ...这是唯一的方法。 document 声明在沙盒 iframe 中设置 document.domain 将失败 - 不,它只是失败 - 因此你不能拥有你的蛋糕(沙箱)并吃掉它(设置 document.domain):p -
请注意,由于you are willing to set this document.domain to allow access between the two contexts,沙盒完全没用:iframe 可以删除自己的沙盒属性。所以正如前面评论中所说,如果它真的是你想要的方式,就不要设置沙箱。但是在你的位置上,我会重新考虑为什么我不能使用 postMessage。过滤少数不可克隆的值并不难。
-
这是有道理的。谢谢!
标签: javascript html