BroadcastChannel

BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames or iframes) of the same origin. Messages are broadcasted via a message event fired at all BroadcastChannel objects listening to the channel.

注意: 此特征可用于 Web 工作者 .

构造函数

BroadcastChannel()
Creates an object linking to the named channel.

特性

This interface also inherits properties from its parent, EventTarget .

BroadcastChannel.name 只读
返回 DOMString , the name of the channel.

事件处理程序

BroadcastChannel.onmessage
An EventHandler property that specifies the function to execute when a message event is fired on this object.
BroadcastChannel.onmessageerror
An EventHandler called when a MessageEvent of type MessageError is fired—that is, when it receives a message that cannot be deserialized.

方法

This interface also inherits methods from its parent, EventTarget .

BroadcastChannel.postMessage()
Sends the message, of any type of object, to each BroadcastChannel object listening to the same channel.
BroadcastChannel.close()
Closes the channel object, indicating it won't get any new messages, and allowing it to be, eventually, garbage collected.

Events

message
Fired when when a message arrives on the channel.
Also available via the onmessage 特性。
messageerror
Fired when a message arrives that can't be deserialized.
Also available via the onmessageerror 特性。

规范

规范 状态 Comment
HTML Living Standard
The definition of 'BroadcastChannel' in that specification.
实时标准 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
BroadcastChannel Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
BroadcastChannel() 构造函数 Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
close Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
message event Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
messageerror event Chrome 60 Edge ≤79 Firefox 57 IE No Opera 47 Safari No WebView Android 60 Chrome Android 60 Firefox Android ? Opera Android 47 Safari iOS No Samsung Internet Android 8.0
name Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
onmessage Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0
onmessageerror Chrome 60 Edge ≤79 Firefox 57 IE No Opera 47 Safari No WebView Android 60 Chrome Android 60 Firefox Android ? Opera Android 44 Safari iOS No Samsung Internet Android 8.0
postMessage Chrome 54 Edge ≤79 Firefox 38 IE No Opera 41 Safari No WebView Android 54 Chrome Android 54 Firefox Android ? Opera Android 41 Safari iOS No Samsung Internet Android 6.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知

另请参阅