BroadcastChannel:postMessage() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.

备注: 此特性在 Web Worker 中可用。

BroadcastChannel 接口的 postMessage() 方法向任何浏览上下文中具有相同的的每个侦听器发送一条消息,该消息可以是任何类型的 Object。该消息以 message 事件的形式传输,该事件针对绑定到频道的每个 BroadcastChannel

语法

js
postMessage(message)

参数

message

要发送到其他窗口的数据。数据使用结构化克隆算法进行序列化。这意味着你可以将各种数据对象安全地传递到目标窗口,而无需自己对其进行序列化。

返回值

无。

异常

InvalidStateError DOMException

如果 BroadcastChannel 已关闭,则抛出此异常。

DataCloneError DOMException

如果输入数据的任何部分不可序列化,则抛出此异常。

规范

Specification
HTML
# dom-broadcastchannel-postmessage-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
postMessage

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
User must explicitly enable this feature.

参见