HTML - sandbox 屬性



sandbox 是一個 HTML 屬性,它可以阻止載入到 iframe 中的文件使用某些功能(例如提交表單或開啟新視窗)。或者,它可以為 iframe 內的內容啟用一組額外的限制。

以下是在 iframe 元素中存在 sandbox 屬性時的功能:

  • 將內容視為來自唯一來源

  • 阻止提交

  • 阻止指令碼執行

  • 停用 API

  • 阻止連結定位其他瀏覽上下文。

  • 阻止內容使用外掛(透過 <embed>、<object>、<applet> 或其他)

  • 阻止內容導航其頂級瀏覽上下文

  • 阻止自動觸發的功能(例如自動播放影片或自動聚焦表單控制元件)

語法

以下是 sandbox 屬性的語法:

<iframe sandbox="value">

屬性值

  • allow-forms - 重新啟用表單提交
  • allow-pointer-lock - 重新啟用 API
  • allow-popups - 重新啟用彈出視窗
  • allow-same-origin - 允許將 iframe 的內容視為來自同一來源
  • allow-scripts - 重新啟用指令碼
  • allow-top-navigation - 允許 iframe 的內容導航其頂級瀏覽上下文

示例

在下面的示例中,我們在 <iframe> 標籤中使用 sandbox 屬性來停用指定網站上的某些功能。

<!DOCTYPE html>
<html>
<head>
   <title>HTML iframe sandbox Attribute</title>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>HTML IFrame sandbox Attribute</h2>
   <br>
   <iframe id="tutFrame" src="https://tutorialspoint.tw/index.htm" width="400" height="200" sandbox></iframe>
</body>
</html>

執行以上程式碼時,它將生成一個包含文字和 tutorialspoint iframe 的網頁輸出。

示例

考慮以下示例,我們在這裡使用 sandbox 屬性及其值 = "allow-forms" 在 <iframe> 標籤中啟用指定網站上的表單提交功能。

<!DOCTYPE html>
<html>
<head>
   <title>HTML iframe sandbox Attribute</title>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>HTML IFrame sandbox Attribute</h2>
   <br>
   <iframe id="tutFrame" src="https://tutorialspoint.tw/market/login.asp" width="400" height="200" sandbox="allow-forms"></iframe>
</body>
</html>

執行以上程式碼後,輸出視窗將彈出,在網頁上顯示 tutorialspoint 登入頁面。

html_attributes_reference.htm
廣告
© . All rights reserved.