BroadcastChannel: BroadcastChannel() 构造函数

Baseline 已广泛支持

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2022 年 3 月起,它已在各浏览器中可用。

注意:此功能在 Web Workers 中可用。

BroadcastChannel() 构造函数创建一个新的 BroadcastChannel 并将其连接到底层通道。

语法

js
new BroadcastChannel(channelName)

参数

channelName

一个字符串,表示通道的名称;对于具有相同 的所有 浏览上下文,此名称只有一个单一通道。

示例

js
// create a new channel listening to the "internal_notification" channel.

const bc = new BroadcastChannel("internal_notification");
bc.postMessage("New listening connected!");

规范

规范
HTML
# dom-broadcastchannel-dev

浏览器兼容性

另见