`
| 屬性 | 說明 | 推薦值/示例 | 備註 |
|---|---|---|---|
src | 嵌入頁面 URL | https://example.com/page.html | 必須同域或目標允許嵌入 |
width/height | 寬高 | 100% / 500px | CSS 同樣生效,優先級更高 |
frameborder | 邊框 (HTML5 廢棄) | 0 (無邊框) | 改用 CSS border: none |
scrolling | 滾動條 (HTML5 廢棄) | no / auto | 改用 CSS overflow |
allowtransparency | 允許透明背景 (IE 專有) | true | 現代瀏覽器默認透明,無需此屬性 |
marginwidth/height | 內邊距 (廢棄) | 0 | 改用 CSS padding (在內部頁面) |
name | 框架名稱 (target 目標) | content-frame | 配合 `` / ` |
<div style="text-align:center;">
<iframe
src="https://weather.265.com/weather.htm"
frameborder="0"
marginwidth="0"
marginheight="0"
scrolling="no"
style="width:168px; height:50px;"
width="168"
height="50"
allowtransparency="true">
</iframe>
</div>
| 屬性 | 作用 | 關鍵值 | 安全等級 |
|---|---|---|---|
sandbox | 啟用沙箱限制 | allow-scripts allow-same-origin allow-forms allow-popups | 🔴 關鍵 |
referrerpolicy | 控制 Referer 頭 | no-referrer / origin / same-origin | 🟡 重要 |
allow | 權限策略 (Feature Policy) | camera 'none'; microphone 'none'; geolocation 'self' | 🟡 重要 |
loading | 懶加載 | lazy / eager | 🟢 性能 |
<iframe
src="https://trusted-partner.com/widget.html"
title="合作夥伴組件" <!-- 無障礙必需 -->
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-modals"
referrerpolicy="origin"
allow="clipboard-read; clipboard-write"
loading="lazy"
style="width:100%; height:400px; border:none; border-radius:8px;"
onload="this.style.opacity=1"
style="opacity:0; transition:opacity 0.3s;"
></iframe>
參考文章 35137 完整方案。核心:同域用 `ResizeObserver`,跨域用 `postMessage`。
目標站點可通過響應頭禁止被嵌入:
# Apache/Nginx
Header always set X-Frame-Options "SAMEORIGIN" # 僅同域可嵌入
Header always set X-Frame-Options "DENY" # 完全禁止嵌入
# CSP 現代替代
Content-Security-Policy: frame-ancestors 'self' https://trusted-partner.com;
嵌入方需確認目標站點允許嵌入,否則會報錯 `Refused to display in a frame`。