BroadcastChannel: messageerror event

当频道收到一条无法反序列化的消息时会在 BroadcastChannel 对象上触发 messageerror 事件。

Bubbles No
Cancelable No
Interface MessageEvent
Event handler property onmessageerror

示例

以下代码使用 addEventListener 来监听消息和错误:

const channel = new BroadcastChannel('example-channel');

channel.addEventListener('message', (event) => {
  received.textContent = event.data;
});

channel.addEventListener('messageerror', (event) => {
  console.error(event);
});

使用 onmessageonmessageerror 事件处理程序来实现相同效果:

const channel = new BroadcastChannel('example-channel');

channel.onmessage = (event) => {
  received.textContent = event.data;
};

channel.onmessageerror = (event) => {
  console.log(event);
};

规范

规范 状态
HTML Living Standard Living Standard

浏览器兼容性

BCD tables only load in the browser

另见