当 BroadcastChannel
接收到类型为 MessageEvent
的 message
事件时,BroadcastChannel.onmessage
属性可以指定一个函数,作为该事件对应的事件处理程序来执行。
Note: 此特性在 Web Worker 中可用。
语法
channel.onmessage = function;
值
function
是一个用户自定义函数的名称,不带()
后续或参数;也可以是一个匿名函数,如function(event) {...}
。一个事件处理程序通常只有一个参数,包含了类型为MessageEvent
的事件。
示例
bc.onmessage = function(ev) { console.log('message event received!'); };
规范
规范 | 状态 | 备注 |
---|---|---|
HTML Living Standard BroadcastChannel.onmessage |
Living Standard | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
另见
- 一种在不同浏览器上下文之间通信的重量级方案:
ServiceWorker
。