以“安全”的方式嵌入其他网站
Embed other site in “safe” way
我想制作一个 iframe 并嵌入一个外部网站,但不允许或极度限制该页面影响父页面的 DOM 和 JavaScript 上下文的能力。有办法吗?
是的!使用 sandbox
property of the iframe
:
// Fully sandboxed
<iframe src="..." sandbox />
// Partially sandboxed, see docs for full set of options
<iframe src="..." sandbox="allow-forms allow-popups" />
请注意,较旧的浏览器(例如 IE 10)不支持此功能,这可能会导致 iframe 在这些浏览器上中断。
,iframe 无法访问父 iframe 跨源的大部分属性
所以它能做的事情很少,尽管它可以将父文档重定向到另一个 URL。
如果您需要进一步限制,可以使用 sandbox
attribute 进一步限制访问。
我想制作一个 iframe 并嵌入一个外部网站,但不允许或极度限制该页面影响父页面的 DOM 和 JavaScript 上下文的能力。有办法吗?
是的!使用 sandbox
property of the iframe
:
// Fully sandboxed
<iframe src="..." sandbox />
// Partially sandboxed, see docs for full set of options
<iframe src="..." sandbox="allow-forms allow-popups" />
请注意,较旧的浏览器(例如 IE 10)不支持此功能,这可能会导致 iframe 在这些浏览器上中断。
所以它能做的事情很少,尽管它可以将父文档重定向到另一个 URL。
如果您需要进一步限制,可以使用 sandbox
attribute 进一步限制访问。