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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
messageerror eventChrome Full support 60Edge Full support ≤79Firefox Full support 57IE No support NoOpera Full support 47Safari No support NoWebView Android Full support 60Chrome Android Full support 60Firefox Android ? Opera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support 8.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

另见