BroadcastChannel

BroadcastChannel 接口代理了一个命名频道,可以让指定 origin 下的任意 browsing context 来订阅它。它允许同源的不同浏览器窗口,Tab页,frame或者 iframe 下的不同文档之间相互通信。通过触发一个 message 事件,消息可以广播到所有监听了该频道的 BroadcastChannel 对象。

Note: 此特性在 Web Worker 中可用。

构造函数

BroadcastChannel()
创建一个链接到命名频道的对象。

属性

该接口会从它的父级 EventTarget 继承属性。

BroadcastChannel.name
频道名称,返回 DOMString

事件处理程序

BroadcastChannel.onmessage
EventHandler ,用于定义当该对象上触发了 message 事件时要执行的函数。
BroadcastChannel.onmessageerror
EventHandler ,用于定义当该对象上触发了类型为 MessageErrorMessageEvent 事件时要执行的函数。当接收到一条无法反序列化的消息时会触发此事件。

方法

该接口会从它的父级 EventTarget 继承方法。

BroadcastChannel.postMessage()

向所有监听了相同频道的 BroadcastChannel 对象发送一条消息,消息内容可以是任意类型的数据。 
BroadcastChannel.close()
关闭频道对象,告诉它不要再接收新的消息,并允许它最终被垃圾回收。

事件

message
当频道收到一条消息时触发。
也可以使用 onmessage 属性访问。
messageerror
当频道收到一条无法反序列化的消息时触发。
也可以使用 onmessageerror 属性访问。

规范

规范 状态 备注
HTML Living Standard
BroadcastChannel
Living Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
BroadcastChannelChrome 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
BroadcastChannel() constructorChrome 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
closeChrome 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
message eventChrome 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
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
nameChrome 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
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
onmessageerrorChrome 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 44Safari iOS No support NoSamsung Internet Android Full support 8.0
postMessageChrome 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

另见