示例工程 16-iframe-embed
16-iframe-embed/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>iframe 与嵌入 · title / sandbox / loading</title>
<meta name="description" content="iframe 的标题、尺寸、懒加载、sandbox 与安全边界。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.75;
}
.page { width: min(100%, 780px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 32px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
.lead { margin: 0 0 24px; color: #334155; font-size: 17px; }
section { margin-top: 28px; padding-top: 20px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 23px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
iframe { display: block; width: 100%; max-width: 560px; height: 220px; border: 1px solid #cbd5e1; border-radius: 10px; background: #fff; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 32px; padding-top: 16px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
.site-header { flex-direction: column; }
nav a + a { margin-left: 12px; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">EMBED NOTES</span>
<nav aria-label="页内导航">
<a href="#basic">基本用法</a>
<a href="#sandbox">sandbox</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 16</p>
<h1>iframe 与嵌入</h1>
<p class="lead">
<code>iframe</code> 在当前页面里再开一个浏览上下文。它能嵌入独立内容,也带来安全、性能与可访问性成本。
</p>
<section id="basic">
<h2>1. 基本用法</h2>
<p>下面嵌入一个本地页面。标题、宽高与懒加载都要写:</p>
<iframe
src="assets/widget.html"
title="产品更新摘要"
width="560"
height="220"
loading="lazy"
>
<p>你的浏览器不支持 iframe。<a href="assets/widget.html">打开嵌入内容</a></p>
</iframe>
<p class="note"><code>title</code> 是 iframe 的可访问名称,读屏会用它说明这个框架是什么。</p>
</section>
<section id="sandbox">
<h2>2. sandbox</h2>
<p>只给必要的权限:脚本可以运行,但不能读父页面。</p>
<iframe
src="assets/sandbox-widget.html"
title="沙箱组件示例"
width="560"
height="180"
loading="lazy"
sandbox="allow-scripts"
referrerpolicy="no-referrer"
>
<p>你的浏览器不支持 iframe。<a href="assets/sandbox-widget.html">打开沙箱示例</a></p>
</iframe>
<ul>
<li><code>sandbox=""</code> 最严格,脚本、表单、弹窗都被禁。</li>
<li><code>allow-scripts</code> 允许脚本,但仍是不透明源。</li>
<li>不要同时给 <code>allow-scripts</code> 与 <code>allow-same-origin</code>,那等于取消隔离。</li>
<li><code>referrerpolicy="no-referrer"</code> 不把来源带给第三方。</li>
</ul>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li>每个 iframe 都要有唯一的 <code>title</code></li>
<li>写 <code>width</code> / <code>height</code>,避免布局跳动</li>
<li>首屏以下用 <code>loading="lazy"</code></li>
<li>第三方内容默认加 <code>sandbox</code></li>
<li>提供回退链接,方便打不开时访问</li>
<li>嵌入内容也要满足可访问性与隐私要求</li>
</ul>
</section>
</main>
<footer>
<p>示例为本地页面,仅用于教学。</p>
</footer>
</div>
</body>
</html>