BroadcastChannel.onmessage

BroadcastChannel 接收到类型为 MessageEventmessage 事件时,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.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onmessageChrome Full support 54Edge Full support ≤79Firefox Full support 38IE No support NoOpera Full support 41Safari No support NoWebView Android Full support 54Chrome Android Full support 54Firefox Android ? Opera Android Full support 41Safari iOS No support NoSamsung Internet Android Full support 6.0

Legend

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

另见

  • 一种在不同浏览器上下文之间通信的重量级方案:ServiceWorker