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> 標籤