HTML <iframe> sandbox 属性
更多“自己尝试”的例子见下文。
定义和用法
sandbox
属性为 iframe 中的内容启用了一组额外的限制。
当 sandbox
属性存在时,它将:
- 将内容视为来自唯一源
- 阻止表单提交
- 阻止脚本执行
- 禁用 API
- 阻止链接定位到其他浏览上下文
- 阻止内容使用插件(通过
<embed>
、<object>
、<applet>
或其他) - 阻止内容导航其顶级浏览上下文
- 阻止自动触发的功能(例如自动播放视频或自动聚焦表单控件)
sandbox
属性的值可以是空的(此时所有限制都将应用),也可以是空格分隔的预定义值列表,这些值将**移除**特定的限制。
浏览器支持
表格中的数字表示完全支持该属性的第一个浏览器版本。
Attribute | |||||
---|---|---|---|---|---|
sandbox | 4.0 | 10.0 | 17.0 | 5.0 | 15.0 |
语法
<iframe sandbox="value">
属性值
值 | 描述 |
---|---|
(无值) | 应用所有限制 |
allow-forms | 允许表单提交 |
allow-modals | 允许打开模态窗口 |
allow-orientation-lock | 允许锁定屏幕方向 |
allow-pointer-lock | 允许使用指针锁定 API |
allow-popups | 允许弹出窗口 |
allow-popups-to-escape-sandbox | 允许弹出窗口打开新窗口而不继承沙盒 |
allow-presentation | 允许启动演示会话 |
allow-same-origin | 允许 iframe 内容被视为与父页面同源 |
allow-scripts | 允许运行脚本 |
allow-top-navigation | 允许 iframe 内容导航其顶级浏览上下文 |
allow-top-navigation-by-user-activation | 允许 iframe 内容导航其顶级浏览上下文,但仅在用户发起时 |
更多示例
示例
允许表单提交的 <iframe> 沙盒
<iframe src="demo_iframe_sandbox_form.htm" sandbox="allow-forms"></iframe>
自己动手试一试 »
示例
允许脚本的 <iframe> 沙盒
<iframe src="demo_iframe_sandbox_origin.htm" sandbox="allow-scripts"></iframe>
自己动手试一试 »
❮ HTML <iframe> 标签