BroadcastChannel: messageerror event
Baseline 2022
Newly available
Since March 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
当频道收到一条无法反序列化的消息时会在 BroadcastChannel
对象上触发 messageerror
事件。
Bubbles | No |
---|---|
Cancelable | No |
Interface | MessageEvent |
Event handler property | onmessageerror |
示例
以下代码使用 addEventListener
来监听消息和错误:
js
const channel = new BroadcastChannel("example-channel");
channel.addEventListener("message", (event) => {
received.textContent = event.data;
});
channel.addEventListener("messageerror", (event) => {
console.error(event);
});
使用 onmessage
和 onmessageerror
事件处理程序来实现相同效果:
js
const channel = new BroadcastChannel("example-channel");
channel.onmessage = (event) => {
received.textContent = event.data;
};
channel.onmessageerror = (event) => {
console.log(event);
};
规范
Specification |
---|
HTML Standard # event-messageerror |
HTML Standard # handler-broadcastchannel-onmessageerror |
浏览器兼容性
BCD tables only load in the browser
参见
- 相关事件:
message