示例工程 16-iframe-embed

16-iframe-embed/demo-02-sandbox/index.html

html · 1.7 KB

运行:sandbox 演示

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>sandbox 演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.75; }
      .box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      iframe { display: block; width: 100%; max-width: 520px; height: 170px; border: 1px solid #cbd5e1; border-radius: 8px; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
    </style>
  </head>
  <body>
    <h1>sandbox 演示</h1>

    <div class="box">
      <h2>sandbox="":全部禁止</h2>
      <iframe src="assets/sandbox-widget.html" title="完全沙箱组件" sandbox width="520" height="170">
        <p>浏览器不支持 iframe。</p>
      </iframe>
      <p>脚本被禁止,页面停在「脚本未运行」。</p>
    </div>

    <div class="box">
      <h2>sandbox="allow-scripts":只放脚本</h2>
      <iframe src="assets/sandbox-widget.html" title="允许脚本的沙箱组件" sandbox="allow-scripts" width="520" height="170">
        <p>浏览器不支持 iframe。</p>
      </iframe>
      <p>脚本运行,但仍是不透明源,不能读父页面。</p>
    </div>

    <div class="box">
      <h2>不要同时给两个权限</h2>
      <p><code>allow-scripts allow-same-origin</code> 会让脚本回到同源,沙箱基本失效。</p>
    </div>
  </body>
</html>