aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/broadcastchannel/index.html
blob: 7ba7c289ea50600ae6f21f6de4a3c2086446ec46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
title: BroadcastChannel
slug: Web/API/BroadcastChannel
tags:
  - API
  - Broadcast Channel API
  - Experimental
  - HTML API
  - Interface
  - Reference
  - TopicStub
translation_of: Web/API/BroadcastChannel
---
<p>{{APIRef("Broadcast Channel API")}}</p>

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

<p>{{AvailableInWorkers}}</p>

<h2 id="构造函数">构造函数</h2>

<dl>
 <dt>{{domxref("BroadcastChannel.BroadcastChannel", "BroadcastChannel()")}}</dt>
 <dd>创建一个链接到命名频道的对象。</dd>
</dl>

<h2 id="属性">属性</h2>

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

<dl>
 <dt>{{domxref("BroadcastChannel.name")}}</dt>
 <dd>频道名称,返回 {{domxref("DOMString")}}</dd>
 <dt>
 <h3 id="事件处理程序">事件处理程序</h3>
 </dt>
 <dt>{{domxref("BroadcastChannel.onmessage")}}</dt>
 <dd>{{domxref("EventHandler")}} ,用于定义当该对象上触发了 {{event("message")}} 事件时要执行的函数。</dd>
 <dt>{{domxref("BroadcastChannel.onmessageerror")}}</dt>
 <dd>{{domxref("EventHandler")}} ,用于定义当该对象上触发了类型为 {{domxref("MessageError")}}{{domxref("MessageEvent")}} 事件时要执行的函数。当接收到一条无法反序列化的消息时会触发此事件。</dd>
</dl>

<h2 id="方法">方法</h2>

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

<p>{{domxref("BroadcastChannel.postMessage()")}}</p>

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

<h2 id="事件">事件</h2>

<dl>
 <dt><code><a href="/en-US/docs/Web/API/BroadcastChannel/message_event">message</a></code></dt>
 <dd>当频道收到一条消息时触发。<br>
 也可以使用 <code><a href="/zh-CN/docs/Web/API/BroadcastChannel/onmessage">onmessage</a></code> 属性访问。</dd>
 <dt><code><a href="/en-US/docs/Web/API/BroadcastChannel/messageerror_event">messageerror</a></code></dt>
 <dd>当频道收到一条无法反序列化的消息时触发。<br>
 也可以使用 <code><a href="/zh-CN/docs/Web/API/BroadcastChannel/onmessageerror">onmessageerror</a></code> 属性访问。</dd>
</dl>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "comms.html#broadcastchannel", "BroadcastChannel")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.BroadcastChannel")}}</p>

<h2 id="另见">另见</h2>

<ul>
 <li>一种在不同浏览器上下文之间通信的重量级方案:{{domxref("ServiceWorker")}}</li>
 <li><a href="/zh-CN/docs/Web/API/Broadcast_Channel_API">Broadcast Channel API overview</a></li>
</ul>