--- title: BroadcastChannel.onmessage slug: Web/API/BroadcastChannel/onmessage translation_of: Web/API/BroadcastChannel/onmessage ---

{{APIRef("BroadCastChannel API")}}

当 {{domxref("BroadcastChannel")}} 接收到类型为 {{domxref("MessageEvent")}} 的 {{event("message")}} 事件时,BroadcastChannel.onmessage 属性可以指定一个函数,作为该事件对应的事件处理程序来执行。

{{AvailableInWorkers}}

语法

channel.onmessage = function;

示例

bc.onmessage = function(ev) { console.log('message event received!'); };

规范

规范 状态 备注
{{SpecName('HTML WHATWG', "comms.html#handler-broadcastchannel-onmessage", "BroadcastChannel.onmessage")}} {{Spec2('HTML WHATWG')}} Initial definition.

浏览器兼容性

{{Compat("api.BroadcastChannel.onmessage")}}

另见