HTML <iframe> sandbox 属性

定义和用法

sandbox 属性为 iframe 中的内容启用了一组额外的限制。

sandbox 属性存在时,它将:

  • 将内容视为来自唯一来源
  • 阻止表单提交
  • 阻止脚本执行
  • 禁用 API
  • 防止链接指向其他浏览上下文
  • 防止内容使用插件(通过 <embed>、<object>、<applet> 或其他方式)
  • 防止内容导航其顶层浏览上下文
  • 阻止自动触发的功能(例如自动播放视频或自动聚焦表单控件)

sandbox 属性的值可以为空(则应用所有限制),或是以空格分隔的预定义值的列表,这将去除特定的限制。

实例

例子 1

带有额外限制的 <iframe>:

<iframe src="demo_iframe_sandbox.htm" sandbox></iframe>

亲自试一试

例子 2

允许表单提交的 <iframe> 沙盒:

<iframe src="demo_iframe_sandbox_form.htm" sandbox="allow-forms"></iframe>

亲自试一试

例子 3

允许脚本的 <iframe> 沙盒:

<iframe src="demo_iframe_sandbox_origin.htm" sandbox="allow-scripts"></iframe>

亲自试一试

语法

<iframe sandbox="value">

属性值

描述
""(没有值) 应用以下所有限制。
allow-forms 允许表单提交。
allow-modals 允许打开模态窗口。
allow-orientation-lock 允许锁定屏幕方向。
allow-pointer-lock Ermöglicht die Verwendung des Pointer Lock API.
allow-popups Ermöglicht das Öffnen von Pop-up-Fenstern.
allow-popups-to-escape-sandbox Ermöglicht das Öffnen von Pop-up-Fenstern in einem neuen Fenster, ohne Sandboxing zu übernehmen.
allow-presentation Ermöglicht den Start einer Präsentationssitzung.
allow-same-origin Ermöglicht, dass iframe-Inhalte als aus derselben Quelle stammend betrachtet werden.
allow-scripts Ermöglicht das Ausführen von Skripts.
allow-top-navigation Ermöglicht die Navigation von iframe-Inhalten in den obersten Browserkontext.
allow-top-navigation-by-user-activation Ermöglicht die Navigation von iframe-Inhalten in den obersten Browserkontext, aber nur, wenn der Benutzer dies initiiert.

Browserunterstützung

Die Zahlen in der Tabelle geben die erste Version des Browsers an, die diese Eigenschaft vollständig unterstützt.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 10.0 17.0 5.0 15.0