首页 > 解决方案 > 如何在沙盒 iframe 上允许 document.domain?

问题描述

我有一个这样定义的沙盒 iframe:

<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals" src="...">
</iframe>

当我document.domain在 iframe 中设置时,出现以下错误:

Uncaught DOMException: Failed to set the 'domain' property on 'Document': Assignment is forbidden for sandboxed iframes.

如何允许在 iframe 中设置 document.domain?

标签: javascripthtml

解决方案


(感谢评论回答我自己的问题)

由于您愿意将 document.domain 设置为允许在 to 上下文之间进行访问,这完全违背了沙箱的目的。因此,首先对 iframe 进行沙盒处理是没有意义的。


推荐阅读