From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/broadcastchannel/index.html | 94 +++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 files/zh-cn/web/api/broadcastchannel/index.html (limited to 'files/zh-cn/web/api/broadcastchannel/index.html') diff --git a/files/zh-cn/web/api/broadcastchannel/index.html b/files/zh-cn/web/api/broadcastchannel/index.html new file mode 100644 index 0000000000..7ba7c289ea --- /dev/null +++ b/files/zh-cn/web/api/broadcastchannel/index.html @@ -0,0 +1,94 @@ +--- +title: BroadcastChannel +slug: Web/API/BroadcastChannel +tags: + - API + - Broadcast Channel API + - Experimental + - HTML API + - Interface + - Reference + - TopicStub +translation_of: Web/API/BroadcastChannel +--- +

{{APIRef("Broadcast Channel API")}}

+ +

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

+ +

{{AvailableInWorkers}}

+ +

构造函数

+ +
+
{{domxref("BroadcastChannel.BroadcastChannel", "BroadcastChannel()")}}
+
创建一个链接到命名频道的对象。
+
+ +

属性

+ +

该接口会从它的父级 {{domxref("EventTarget")}} 继承属性。

+ +
+
{{domxref("BroadcastChannel.name")}}
+
频道名称,返回 {{domxref("DOMString")}} 。
+
+

事件处理程序

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

方法

+ +

该接口会从它的父级 {{domxref("EventTarget")}} 继承方法。

+ +

{{domxref("BroadcastChannel.postMessage()")}}

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

事件

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

规范

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

浏览器兼容性

+ + + +

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

+ +

另见

+ + -- cgit v1.2.3-54-g00ecf