diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/messagechannel | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/messagechannel')
-rw-r--r-- | files/zh-cn/web/api/messagechannel/index.html | 87 | ||||
-rw-r--r-- | files/zh-cn/web/api/messagechannel/messagechannel/index.html | 79 | ||||
-rw-r--r-- | files/zh-cn/web/api/messagechannel/port1/index.html | 148 | ||||
-rw-r--r-- | files/zh-cn/web/api/messagechannel/port2/index.html | 144 |
4 files changed, 458 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/messagechannel/index.html b/files/zh-cn/web/api/messagechannel/index.html new file mode 100644 index 0000000000..47cb52146f --- /dev/null +++ b/files/zh-cn/web/api/messagechannel/index.html @@ -0,0 +1,87 @@ +--- +title: MessageChannel +slug: Web/API/MessageChannel +tags: + - API + - Channel Messaging API + - Interface + - MessageChannel + - Reference + - web messaging +translation_of: Web/API/MessageChannel +--- +<p>{{APIRef("HTML DOM")}}</p> + +<p>Channel Messaging API的<strong><code>MessageChannel</code></strong> 接口允许我们创建一个新的消息通道,并通过它的两个{{domxref("MessagePort")}} 属性发送数据。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("MessageChannel.port1")}} {{readonlyInline}}</dt> + <dd>返回channel的port1。</dd> + <dt>{{domxref("MessageChannel.port2")}} {{readonlyInline}}</dt> + <dd>返回channel的port2。</dd> +</dl> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("MessageChannel.MessageChannel", "MessageChannel()")}}</dt> + <dd> + <p>返回一个带有两个MessagePort属性的MessageChannel新对象。</p> + </dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>在以下代码块中,您可以看到使用MessageChannel构造函数实例化了一个channel对象。当iframe加载完毕,我们使用MessagePort.postMessage方法把一条消息和MessageChannel.port2传递给iframe。handleMessage处理程序将会从iframe中(使用MessagePort.onmessage监听事件)接收到信息,将数据其放入innerHTML中。</p> + +<pre class="brush: js">var channel = new MessageChannel(); +var para = document.querySelector('p'); + +var ifr = document.querySelector('iframe'); +var otherWindow = ifr.contentWindow; + +ifr.addEventListener("load", iframeLoaded, false); + +function iframeLoaded() { + otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]); +} + +channel.port1.onmessage = handleMessage; +function handleMessage(e) { + para.innerHTML = e.data; +} </pre> + +<p>一个完整的运行示例,可以在Github上查看 <a class="external external-icon" href="https://github.com/mdn/dom-examples/tree/master/channel-messaging-basic">channel messaging basic demo</a> (<a class="external external-icon" href="https://mdn.github.io/dom-examples/channel-messaging-basic/">run it live too</a>).</p> + +<h2 id="Specifications规范">Specifications/规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', '#message-channels','MessageChannel')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>No difference from {{SpecName("HTML5 Web Messaging")}}.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{Compat("api.MessageChannel")}}</div> + +<div id="compat-mobile"></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/messagechannel/messagechannel/index.html b/files/zh-cn/web/api/messagechannel/messagechannel/index.html new file mode 100644 index 0000000000..36a4ff6043 --- /dev/null +++ b/files/zh-cn/web/api/messagechannel/messagechannel/index.html @@ -0,0 +1,79 @@ +--- +title: MessageChannel() +slug: Web/API/MessageChannel/MessageChannel +tags: + - API + - Channel messaging + - Constructor + - MessageChannel + - 参考 + - 构造函数 +translation_of: Web/API/MessageChannel/MessageChannel +--- +<p>{{APIRef("HTML DOM")}}</p> + +<p>{{domxref("MessageChannel")}} 接口的 <code>MessageChannel()</code> 构造函数返回一个新的 {{domxref("MessageChannel")}} 对象,返回的对象中包含两个 {{domxref("MessagePort")}} 对象。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var channel = new MessageChannel();</pre> + +<h3 id="返回值">返回值</h3> + +<p>一个新创建的 {{domxref("MessageChannel")}} 对象。</p> + +<h2 id="例子">例子</h2> + +<p>在下面的代码块中,你会看到一个由 {{domxref("MessageChannel()", "MessageChannel.MessageChannel")}} 构造函数创建的新 Channel. 当 IFrame 被加载后,我们使用 {{domxref("MessagePort.postMessage")}} 把 <code>port2</code> 和一条消息一起发送给 IFrame. 然后 <code>handleMessage</code> 回调响应 IFrame 发回的消息(使用 {{domxref("MessagePort.onmessage")}}),并把它渲染到页面段落中。{{domxref("MessageChannel.port1")}} 用来监听,当消息到达时,会进行处理。</p> + +<pre class="brush: js notranslate">var channel = new MessageChannel(); +var para = document.querySelector('p'); + +var ifr = document.querySelector('iframe'); +var otherWindow = ifr.contentWindow; + +ifr.addEventListener("load", iframeLoaded, false); + +function iframeLoaded() { + otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]); +} + +channel.port1.onmessage = handleMessage; +function handleMessage(e) { + para.innerHTML = e.data; +} </pre> + +<p>要查看完整可运行的例子,参考我们在 Github 上的 <a class="external external-icon" href="https://github.com/mdn/channel-messaging-basic-demo">channel messaging basic demo</a> (<a class="external external-icon" href="http://mdn.github.io/channel-messaging-basic-demo/">在线运行</a>)。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', 'web-messaging.html#dom-messagechannel', 'MessageChannel()')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.MessageChannel.MessageChannel")}}</p> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging">使用 channel messaging</a></li> +</ul> diff --git a/files/zh-cn/web/api/messagechannel/port1/index.html b/files/zh-cn/web/api/messagechannel/port1/index.html new file mode 100644 index 0000000000..810f29d780 --- /dev/null +++ b/files/zh-cn/web/api/messagechannel/port1/index.html @@ -0,0 +1,148 @@ +--- +title: MessageChannel.port1 +slug: Web/API/MessageChannel/port1 +translation_of: Web/API/MessageChannel/port1 +--- +<div>{{APIRef("HTML DOM")}}</div> + +<p>{{domxref("MessageChannel")}} 的只读属性 <code><strong>port1 </strong>返回消息通道的第一个端口</code>, 此端口连接到源上下文通道。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">channel.port1;</pre> + +<h3 id="Value">Value</h3> + +<p>一个 {{domxref("MessagePort")}} 对象, 通道的第一个端口,此端口连接到源上下文通道。</p> + +<h2 id="示例">示例</h2> + +<p>在以下代码块中,您可以看到使用 {{domxref("MessageChannel.MessageChannel", "MessageChannel()")}}构造函数创建的新通道。当{{HTMLElement("iframe")}}加载完毕,我们使用{{domxref("MessagePort.postMessage")}}方法把一条消息和{{domxref("MessageChannel.port2")}}传递给{{HTMLElement("iframe")}}。handleMessage处理程序将会从<iframe>中(使用{{domxref("MessagePort.onmessage")}}监听事件)接收到信息,将数据其放入一个段落。handleMessage 方法关联到 port1用于监听收到的消息。</p> + +<pre class="brush: js;highlight[13]">var channel = new MessageChannel(); +var para = document.querySelector('p'); + +var ifr = document.querySelector('iframe'); +var otherWindow = ifr.contentWindow; + +ifr.addEventListener("load", iframeLoaded, false); + +function iframeLoaded() { + otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]); +} + +channel.port1.onmessage = handleMessage; +function handleMessage(e) { + para.innerHTML = e.data; +} +</pre> + +<p>一个完整的运行示例,可以在Github上查看 <a class="external external-icon" href="https://github.com/mdn/channel-messaging-basic-demo">channel messaging basic demo</a> (<a class="external external-icon" href="http://mdn.github.io/channel-messaging-basic-demo/">run it live too</a>).</p> + +<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', '#dom-messagechannel-port1','port1')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>No difference from {{SpecName("HTML5 Web Messaging")}}.</td> + </tr> + <tr> + <td>{{SpecName('HTML5 Web Messaging', '#dom-messagechannel-port1','port1')}}</td> + <td>{{Spec2('HTML5 Web Messaging')}}</td> + <td>W3C version of the spec</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>4</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(41)}}</td> + <td>10.0</td> + <td>10.6</td> + <td>5</td> + </tr> + <tr> + <td>Available in workers</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.4</td> + <td>4</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>10.0</td> + <td>11.5</td> + <td>5.1</td> + </tr> + <tr> + <td>Available in workers</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</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/messagechannel/port2/index.html b/files/zh-cn/web/api/messagechannel/port2/index.html new file mode 100644 index 0000000000..7f4a3b7079 --- /dev/null +++ b/files/zh-cn/web/api/messagechannel/port2/index.html @@ -0,0 +1,144 @@ +--- +title: MessageChannel.port2 +slug: Web/API/MessageChannel/port2 +translation_of: Web/API/MessageChannel/port2 +--- +<p>{{APIRef("HTML DOM")}}</p> + +<p>{{domxref("MessageChannel")}}接口的 <code><strong>port2</strong></code> 是一个只读属性,返回消息通道的第二个端口,该端口连接到通道另一端的上下文,也就是发送消息时的目的地。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">channel.port2;</pre> + +<h3 id="值">值</h3> + +<p>表示通道第二个端口的一个{{domxref("MessagePort")}}对象,该端口附加到通道另一端的上下文。</p> + +<p> </p> + +<h2 id="示例">示例</h2> + +<p>如下代码所示,通过{{domxref("MessageChannel()", "MessageChannel.MessageChannel")}}构造函数创建了一个新的通道。当IFrame加载完毕,我们使用{{domxref("MessagePort.postMessage")}}将一条消息以及port2传递给IFrame。handleMessage处理程序响应从IFrame发回的消息(使用 {{domxref("MessagePort.onmessage")}}),将其放入段落中。{{domxref("MessageChannel.port1")}}已经监听,以检测消息何时到达。</p> + +<pre class="brush: js">var channel = new MessageChannel(); +var para = document.querySelector('p'); + +var ifr = document.querySelector('iframe'); +var otherWindow = ifr.contentWindow; + +ifr.addEventListener("load", iframeLoaded, false); + +function iframeLoaded() { + otherWindow.postMessage('Hello from the main page!', '*', [channel.port2]); +} + +channel.port1.onmessage = handleMessage; +function handleMessage(e) { + para.innerHTML = e.data; +} </pre> + +<p>有关完整的运行示例,请参阅我们在GitHub的 <a class="external external-icon" href="https://github.com/mdn/channel-messaging-basic-demo">channel messaging basic demo</a> (<a class="external external-icon" href="http://mdn.github.io/channel-messaging-basic-demo/">run it live too</a>).</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', 'web-messaging.html#dom-messagechannel-port2', 'port2')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>4</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop(41)}}</td> + <td>10.0</td> + <td>10.6</td> + <td>5</td> + </tr> + <tr> + <td>Available in workers</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Edge</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.4</td> + <td>4</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>10.0</td> + <td>11.5</td> + <td>5.1</td> + </tr> + <tr> + <td>Available in workers</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(41)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging">Using channel messaging</a></li> +</ul> |