aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/broadcastchannel
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/broadcastchannel
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/broadcastchannel')
-rw-r--r--files/zh-cn/web/api/broadcastchannel/broadcastchannel/index.html58
-rw-r--r--files/zh-cn/web/api/broadcastchannel/close/index.html55
-rw-r--r--files/zh-cn/web/api/broadcastchannel/index.html94
-rw-r--r--files/zh-cn/web/api/broadcastchannel/messageerror_event/index.html83
-rw-r--r--files/zh-cn/web/api/broadcastchannel/name/index.html57
-rw-r--r--files/zh-cn/web/api/broadcastchannel/onmessage/index.html55
-rw-r--r--files/zh-cn/web/api/broadcastchannel/onmessageerror/index.html45
-rw-r--r--files/zh-cn/web/api/broadcastchannel/postmessage/index.html44
8 files changed, 491 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/broadcastchannel/broadcastchannel/index.html b/files/zh-cn/web/api/broadcastchannel/broadcastchannel/index.html
new file mode 100644
index 0000000000..2eafc86263
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/broadcastchannel/index.html
@@ -0,0 +1,58 @@
+---
+title: BroadcastChannel()
+slug: Web/API/BroadcastChannel/BroadcastChannel
+translation_of: Web/API/BroadcastChannel/BroadcastChannel
+---
+<p>{{APIRef("BroadCastChannel API")}}</p>
+
+<p><code><strong>BroadcastChannel()</strong></code> 构建函数用于创建一个 {{domxref("BroadcastChannel")}} 对象,并与对应的频道相关联。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"> <em>channel</em> = new BroadcastChannel(<em>channel</em>);</pre>
+
+<h3 id="值">值</h3>
+
+<dl>
+ <dt><em>channel</em></dt>
+ <dd>频道名称,类型为 {{domxref("DOMString")}};在相同的 {{glossary("origin", "源")}} 下,一个名称只对应一个频道,所有 {{glossary("browsing context", "浏览器上下文")}} 共用。 </dd>
+</dl>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js">// create a new channel listening to the "internal_notification" channel.
+
+var bc = new BroadcastChannel('internal_notification');
+bc.postMessage('New listening connected!');
+</pre>
+
+<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#dom-broadcastchannel", "BroadcastChannel()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.BroadcastChannel")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li>所属接口:{{domxref("BroadcastChannel")}}</li>
+</ul>
diff --git a/files/zh-cn/web/api/broadcastchannel/close/index.html b/files/zh-cn/web/api/broadcastchannel/close/index.html
new file mode 100644
index 0000000000..e3eb201840
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/close/index.html
@@ -0,0 +1,55 @@
+---
+title: BroadcastChannel.close()
+slug: Web/API/BroadcastChannel/close
+translation_of: Web/API/BroadcastChannel/close
+---
+<p>{{APIRef("BroadCastChannel API")}}</p>
+
+<p>通过调用 <strong><code>BroadcastChannel.close()</code></strong> 方法,可以马上断开其与对应频道的关联,并让其被垃圾回收。这是必要的步骤,因为浏览器没有其它方式知道频道不再被需要。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <em>str</em> = <em>channe<code>l</code></em><code>.close();</code>
+</pre>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js">// 连接到指定频道
+var bc = new BroadcastChannel('test_channel');
+
+// 其它操作 (如: postMessage, …)
+
+// 当完成后,断开与频道的连接
+bc.close();
+</pre>
+
+<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#dom-broadcastchannel-close", "BroadcastChannel.close()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.close")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li>{{domxref("BroadcastChannel")}}, the interface it belongs to.</li>
+</ul>
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
+---
+<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>
diff --git a/files/zh-cn/web/api/broadcastchannel/messageerror_event/index.html b/files/zh-cn/web/api/broadcastchannel/messageerror_event/index.html
new file mode 100644
index 0000000000..22b1a722f2
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/messageerror_event/index.html
@@ -0,0 +1,83 @@
+---
+title: 'BroadcastChannel: messageerror event'
+slug: Web/API/BroadcastChannel/messageerror_event
+translation_of: Web/API/BroadcastChannel/messageerror_event
+---
+<div>{{APIRef}}</div>
+
+<p>当频道收到一条无法反序列化的消息时会在 {{domxref('BroadcastChannel')}} 对象上触发 <code>messageerror</code> 事件。</p>
+
+<table class="properties">
+ <tbody>
+ <tr>
+ <th scope="row">Bubbles</th>
+ <td>No</td>
+ </tr>
+ <tr>
+ <th scope="row">Cancelable</th>
+ <td>No</td>
+ </tr>
+ <tr>
+ <th scope="row">Interface</th>
+ <td>{{domxref("MessageEvent")}}</td>
+ </tr>
+ <tr>
+ <th scope="row">Event handler property</th>
+ <td><code><a href="/en-US/docs/Web/API/BroadcastChannel/onmessageerror">onmessageerror</a></code></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="示例">示例</h2>
+
+<p>以下代码使用 <code><a href="/en-US/docs/Web/API/EventTarget/addEventListener">addEventListener</a></code> 来监听消息和错误:</p>
+
+<pre class="brush: js">const channel = new BroadcastChannel('example-channel');
+
+channel.addEventListener('message', (event) =&gt; {
+ received.textContent = event.data;
+});
+
+channel.addEventListener('messageerror', (event) =&gt; {
+ console.error(event);
+});</pre>
+
+<p>使用 <code><a href="/en-US/docs/Web/API/BroadcastChannel/onmessage">onmessage</a></code> 和 <code><a href="/en-US/docs/Web/API/BroadcastChannel/onmessageerror">onmessageerror</a></code> 事件处理程序来实现相同效果:</p>
+
+<pre class="brush: js">const channel = new BroadcastChannel('example-channel');
+
+channel.onmessage = (event) =&gt; {
+ received.textContent = event.data;
+};
+
+channel.onmessageerror = (event) =&gt; {
+ console.log(event);
+};
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('HTML WHATWG', 'indices.html#event-messageerror')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.messageerror_event")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li>相关事件:<code><a href="/docs/Web/API/BroadcastChannel/message_event">message</a></code></li>
+</ul>
diff --git a/files/zh-cn/web/api/broadcastchannel/name/index.html b/files/zh-cn/web/api/broadcastchannel/name/index.html
new file mode 100644
index 0000000000..d6e21fd6e3
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/name/index.html
@@ -0,0 +1,57 @@
+---
+title: BroadcastChannel.name
+slug: Web/API/BroadcastChannel/name
+translation_of: Web/API/BroadcastChannel/name
+---
+<p>{{APIRef("BroadCastChannel API")}}</p>
+
+<p><code><strong>BroadcastChannel.name</strong></code> 是类型为 {{domxref("DOMString")}} 的只读属性,是频道的唯一标识。属性 name 是在创建时传入 {{domxref("BroadcastChannel.BroadCastChannel", "BroadcastChannel()")}} 构造函数的,所以是只读的。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <em>str</em> = <em>channe<code>l</code></em><code>.name;</code>
+</pre>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js">// 连接到指定频道
+var bc = new BroadcastChannel('test_channel');
+
+// 其它操作 (如: postMessage, …)
+
+// 在控制台打印频道名称
+console.log(bc.name); // "test_channel"
+
+// 当完成后,断开与频道的连接
+bc.close();</pre>
+
+<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#dom-broadcastchannel-name", "BroadcastChannel.name")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.name")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>所属接口:{{domxref("BroadcastChannel")}}</li>
+</ul>
diff --git a/files/zh-cn/web/api/broadcastchannel/onmessage/index.html b/files/zh-cn/web/api/broadcastchannel/onmessage/index.html
new file mode 100644
index 0000000000..ffea7d9825
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/onmessage/index.html
@@ -0,0 +1,55 @@
+---
+title: BroadcastChannel.onmessage
+slug: Web/API/BroadcastChannel/onmessage
+translation_of: Web/API/BroadcastChannel/onmessage
+---
+<p>{{APIRef("BroadCastChannel API")}}</p>
+
+<p>当 {{domxref("BroadcastChannel")}} 接收到类型为 {{domxref("MessageEvent")}} 的 {{event("message")}} 事件时,<code><strong>BroadcastChannel.onmessage</strong></code> 属性可以指定一个函数,作为该事件对应的事件处理程序来执行。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"><em>channel</em>.onmessage = <em>function</em>;
+</pre>
+
+<h3 id="值">值</h3>
+
+<ul>
+ <li><code>function</code> 是一个用户自定义函数的名称,不带 <code>()</code> 后续或参数;也可以是一个匿名函数,如 <code>function(event) {...}</code>。一个事件处理程序通常只有一个参数,包含了类型为 {{domxref("MessageEvent")}} 的事件。</li>
+</ul>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js">bc.onmessage = function(ev) { console.log('message event received!'); };
+</pre>
+
+<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#handler-broadcastchannel-onmessage", "BroadcastChannel.onmessage")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.onmessage")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li>一种在不同浏览器上下文之间通信的重量级方案:{{domxref("ServiceWorker")}}。</li>
+</ul>
diff --git a/files/zh-cn/web/api/broadcastchannel/onmessageerror/index.html b/files/zh-cn/web/api/broadcastchannel/onmessageerror/index.html
new file mode 100644
index 0000000000..01c0e9ca2a
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/onmessageerror/index.html
@@ -0,0 +1,45 @@
+---
+title: BroadcastChannel.onmessageerror
+slug: Web/API/BroadcastChannel/onmessageerror
+translation_of: Web/API/BroadcastChannel/onmessageerror
+---
+<div>{{APIRef("HTML DOM")}}</div>
+
+<p>位于 {{domxref("BroadcastChannel")}} 接口上的 <code><strong>onmessageerror</strong></code> 事件处理程序,它是一个 {{domxref("EventListener", "事件监听器")}}。当  <code>BroadcastChannel</code> 接收到一条无法 {{glossary("Deserialization", "反序列化")}}的消息时,会触发类型为 {{domxref("MessageError")}} 的 {{domxref("MessageEvent")}} 事件,此时会执行该事件处理程序。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">bc.onmessageerror = function() { ... };</pre>
+
+<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', '#handler-broadcastchannel-onmessageerror', 'onmessageerror')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>
+
+
+<p>{{Compat("api.BroadcastChannel.onmessageerror")}}</p>
+</div>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging">Using channel messaging</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/broadcastchannel/postmessage/index.html b/files/zh-cn/web/api/broadcastchannel/postmessage/index.html
new file mode 100644
index 0000000000..374c611b34
--- /dev/null
+++ b/files/zh-cn/web/api/broadcastchannel/postmessage/index.html
@@ -0,0 +1,44 @@
+---
+title: BroadcastChannel.postMessage()
+slug: Web/API/BroadcastChannel/postMessage
+translation_of: Web/API/BroadcastChannel/postMessage
+---
+<p>{{APIRef("BroadCastChannel API")}}</p>
+
+<p>可以使用 <strong><code>BroadcastChannel.postMessage()</code></strong> 发送一条任意 {{jsxref("Object")}} 类型的消息,给所有同{{glossary("origin", "源")}}下监听了该频道的所有{{glossary("browsing context", "浏览器上下文")}}。消息以 {{event("message")}} 事件的形式发送给每一个绑定到该频道的广播频道。</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <em>str</em> = <em>channe<code>l</code></em><code>.postMessage(<em>object</em>);</code>
+</pre>
+
+<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#dom-broadcastchannel-postmessage", "BroadcastChannel.postmessage()")}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.BroadcastChannel.postMessage")}}</p>
+
+<h2 id="另见">另见</h2>
+
+<ul>
+ <li>所属接口:{{domxref("BroadcastChannel")}}</li>
+</ul>