Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
メモ: この機能はウェブワーカー内で利用可能です。
MessageChannel はチャンネルメッセージング API のインターフェイスで、新しいメッセージチャンネルを作成し、2 つの MessagePort プロパティを通して、その間でデータを送信できます。
2 つの新しい MessagePort オブジェクトを持つ新しい MessageChannel オブジェクトを返します。
チャンネルの port1 を返します。
MessageChannel.port2 読取専用チャンネルの port2 を返します。
次の例では、MessageChannel() コンストラクターを使用して新しいチャンネルを作成する様子を見ることができます。
iframe が読み込まれると、onmessage ハンドラーを MessageChannel.port1 に登録し、MessageChannel.port2 を window.postMessage メソッドを使用して iframe へ転送します。
IFrame からメッセージが返送されると、onMessage 関数はそのメッセージを段落に出力します。
完全に動作する例は、Github 上の channel messaging basic demo を参照してください (実際のデモも実行できます)。
| HTML # message-channels |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2024年8月20日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.