頁面使用 `
// 方式一:window.parent
const parentEl = window.parent.document.getElementById('parentElementId');
// 方式二:window.top (最頂層視窗)
const topEl = window.top.document.getElementById('topElementId');
// 方式三:特定 name 的 sibling frame
const siblingEl = window.parent.frames['frameName'].document.getElementById('elId');
// 方式一:iframe 元素的 contentWindow / contentDocument
const iframe = document.getElementById('myIframe');
const childDoc = iframe.contentDocument || iframe.contentWindow.document;
const childEl = childDoc.getElementById('childElementId');
// 方式二:window.frames 集合 (以 name 或 index)
const childEl2 = window.frames['myIframeName'].document.getElementById('childElementId');
const childEl3 = window.frames[0].document.getElementById('childElementId');
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Parent</title></head>
<body>
<h1>父頁面</h1>
<input type="text" id="parentInput" value="來自父頁">
<button id="btnReadChild">讀取子頁面輸入</button>
<button id="btnWriteChild">寫入子頁面輸入</button>
<iframe id="childFrame" src="child.html" style="width:100%;height:200px;border:1px solid #ccc;"></iframe>
<script>
const iframe = document.getElementById('childFrame');
// 等待 iframe 載入完成
iframe.onload = () => {
const childDoc = iframe.contentDocument || iframe.contentWindow.document;
document.getElementById('btnReadChild').onclick = () => {
const val = childDoc.getElementById('childInput').value;
alert('子頁面輸入:' + val);
};
document.getElementById('btnWriteChild').onclick = () => {
childDoc.getElementById('childInput').value = '父頁寫入 ' + Date.now();
};
};
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>Child</title></head>
<body style="background:#f9f9f9;">
<h2>子頁面</h2>
<input type="text" id="childInput" value="來自子頁">
<button id="btnReadParent">讀取父頁面輸入</button>
<button id="btnWriteParent">寫入父頁面輸入</button>
<script>
document.getElementById('btnReadParent').onclick = () => {
const val = window.parent.document.getElementById('parentInput').value;
alert('父頁面輸入:' + val);
};
document.getElementById('btnWriteParent').onclick = () => {
window.parent.document.getElementById('parentInput').value = '子頁寫入 ' + Date.now();
};
</script>
</body>
</html>
| 方案 | 原理 | 適用場景 | 備註 |
|---|---|---|---|
postMessage | 瀏覽器原生跨域通訊 API,透過事件傳遞序列化資料 | **首選**,所有現代瀏覽器支援,安全可控 | 需雙方協定訊息格式、驗證 origin |
document.domain 降級 | 將主域名設為相同 (如 a.example.com, b.example.com → example.com) | **已棄用**,Chrome 100+ 移除支援,僅限同主域子域 | 不可用於不同主域 |
| CORS + 共享儲存 | 後端提供 API,前端用 localStorage/IndexedDB 同步 | 複雜狀態同步 | 非即時、儲存配額限制 |
| 代理 Iframe | 嵌入同源代理頁,代理再與目標通訊 | 舊系統整合 | 複雜度高、效能差 |
// 父頁面
iframe.contentWindow.postMessage({type: 'SET_VALUE', payload: 'hello'}, 'https://child.example.com');
window.addEventListener('message', (e) => {
if (e.origin !== 'https://child.example.com') return; // 驗證來源
if (e.data.type === 'VALUE_RESPONSE') console.log('子頁回應:', e.data.payload);
});
// 子頁面
window.addEventListener('message', (e) => {
if (e.origin !== 'https://parent.example.com') return;
if (e.data.type === 'SET_VALUE') {
document.getElementById('childInput').value = e.data.payload;
e.source.postMessage({type: 'VALUE_RESPONSE', payload: 'OK'}, e.origin);
}
});