JavaScript window.open():打开窗口并在父子窗口间传值
在点击事件处理函数中调用 window.open(),然后用返回的窗口引用和 postMessage() 双向传值。示例中的子窗口会先通知父窗口自己已准备好,双方收到消息后都会检查来源、发送窗口和数据。
打开窗口或标签页
在 window.open(url, target, features) 中,target 可以是可复用的窗口名称,也可以是 _blank 等关键字。popup,width=420,height=320 请求打开一个小弹窗。浏览器也可能打开标签页或调整大小。请直接在点击事件处理函数中调用此方法。像示例这样未指定 noopener 时,如果返回 null,应处理窗口未能打开的情况。 HTML Standard · window.open()
核对消息来源和发送窗口
源由协议、主机和端口组成。例如,http://localhost:8000 和 http://127.0.0.1:8000 属于不同的源。不同源的窗口无法直接访问对方的 DOM。使用 postMessage(data, targetOrigin) 时应指定接收方的确切源,接收时则要验证 event.origin、event.source、消息类别以及值的数据类型。 HTML Standard · postMessage()
用两个文件双向传值
将以下两段代码保存在同一目录中,分别命名为 parent.html 和 child.html。安装 Python 3 后,在该目录运行下面的命令,然后打开 http://127.0.0.1:8000/parent.html。由同一地址提供这两个文件,可以让两个页面的 location.origin 保持一致。
python -m http.server 8000 --bind 127.0.0.1
parent.html — 父窗口收到 ready 后,将此时输入框中的值放进 initial 消息并发送给子窗口。
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>父窗口</title>
</head>
<body>
<label for="value">父窗口的值</label>
<input id="value" value="初始值">
<button id="open" type="button">打开子窗口</button>
<p id="status" role="status"></p>
<script>
const input = document.querySelector('#value');
const status = document.querySelector('#status');
const origin = location.origin;
let child = null;
document.querySelector('#open').addEventListener('click', () => {
child = window.open('child.html', 'valueExample', 'popup,width=420,height=320');
status.textContent = child ? "正在等待子窗口响应。" : "无法打开弹窗。";
});
window.addEventListener('message', event => {
if (!child || event.origin !== origin || event.source !== child) return;
const data = event.data;
if (data?.type === 'ready') {
child.postMessage({ type: 'initial', value: input.value }, origin);
} else if (data?.type === 'result' && typeof data.value === 'string') {
input.value = data.value;
status.textContent = "已收到子窗口发送的值。";
}
});
</script>
</body>
</html>
child.html — 子窗口收到初始值后才启用发送按钮。点击按钮会将编辑后的字符串放进 result 消息并发回父窗口。
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>子窗口</title>
</head>
<body>
<label for="child-value">子窗口的值</label>
<input id="child-value">
<button id="send" type="button" disabled>发送给父窗口</button>
<p id="child-status" role="status"></p>
<script>
const input = document.querySelector('#child-value');
const send = document.querySelector('#send');
const status = document.querySelector('#child-status');
const origin = location.origin;
const openerWindow = window.opener;
if (!openerWindow) {
status.textContent = "请通过父窗口的按钮打开此页面。";
} else {
window.addEventListener('message', event => {
if (event.origin !== origin || event.source !== openerWindow) return;
const data = event.data;
if (data?.type !== 'initial' || typeof data.value !== 'string') return;
input.value = data.value;
status.textContent = "已收到父窗口发送的值。";
send.disabled = false;
});
openerWindow.postMessage({ type: 'ready' }, origin);
}
send.addEventListener('click', () => {
if (send.disabled) return;
if (openerWindow.closed) {
send.disabled = true;
status.textContent = "父窗口已关闭。";
return;
}
openerWindow.postMessage({ type: 'result', value: input.value }, origin);
status.textContent = "已向父窗口发送值。";
});
</script>
</body>
</html>
在父窗口中输入 123,再打开子窗口,子窗口的输入框应显示 123。将它改成 456 并发送,父窗口的输入框也应变为 456。练习结束后,关闭子窗口,并在运行服务器的终端按 Ctrl+C。
收不到值时检查什么
窗口未打开时,检查浏览器的弹窗设置,再点击按钮重试。如果发送按钮一直不可用,请确认两个页面同源,并且子窗口是通过父窗口中的按钮打开的。关闭父窗口后尝试发送,子窗口会显示提示。
示例保留了与打开方的连接,方便两个文档通信。如果外部页面无需通信,可在链接中使用 rel="noopener",或在 window.open() 中指定 noopener 来断开连接。即使标签页成功打开,使用该选项也可能返回 null。文档隔离策略也可能切断该连接。




