MessageChannel:port2 属性

备注: 此特性在 Web Worker 中可用。

MessageChannel 接口的 port2 只读属性返回消息 channel 的第二个端口——即连接到通道另一端上下文的端口,消息最初将发送到这个端口。

一个 MessagePort 对象,它是 channel 的第二个端口,即连接到 channel 另一端上下文的端口。

示例

在以下的代码块中,你可以看到使用 MessageChannel() 构造函数创建的新 Channel。当 IFrame 加载完成后,我们使用 MessagePort.postMessageport2 传递给 IFrame,并附带一条消息。然后 handleMessage 处理程序响应从 IFrame 发送回来的消息(使用 onmessage),并将其放入一个段落中。同时监听 port1 以检查何时接收到消息。

js
const channel = new MessageChannel();
const para = document.querySelector("p");

const ifr = document.querySelector("iframe");
const otherWindow = ifr.contentWindow;

ifr.addEventListener("load", iframeLoaded, false);

function iframeLoaded() {
  otherWindow.postMessage("来自主页的问候!", "*", [channel.port2]);
}

channel.port1.onmessage = handleMessage;
function handleMessage(e) {
  para.innerHTML = e.data;
}

有关完整的运行示例,请参阅我们在 GitHub 上的 channel messaging 基础示例也可以实时运行它

规范

Specification
HTML Standard
# dom-messagechannel-port2-dev

浏览器兼容性

BCD tables only load in the browser

参见