Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

BroadcastChannel: message event

BaselineWidely available

Note: This feature is available inWeb Workers.

Themessage event of theBroadcastChannel interface fires when a message arrives on that channel.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("message", (event) => { })onmessage = (event) => { }

Event type

Event properties

In addition to the properties listed below, properties from the parent interface,Event, are available.

dataRead only

The data sent by the message emitter.

originRead only

A string representing the origin of the message emitter.

lastEventIdRead only

A string representing a unique ID for the event.

sourceRead only

Amessage event source, which is either aWindowProxy, aMessagePort, or aServiceWorker object representing the message emitter.

portsRead only

An array ofMessagePort objects representing the ports associated with the channel the message is being sent through (where appropriate, e.g., in channel messaging or when sending a message to a shared worker).

Examples

In this example there's a "sender"<iframe> that broadcasts the contents of a<textarea> when the user clicks a button. There are two "receiver" iframes that listen to the broadcast message and write the result into a<div> element.

Sender

<h1>Sender</h1><label for="message">Type a message to broadcast:</label><br /><textarea name="message" rows="1" cols="40">Hello</textarea><button type="button">Broadcast message</button>
body {  border: 1px solid black;  padding: 0.5rem;  height: 150px;  font-family: "Fira Sans", sans-serif;}h1 {  font:    1.6em "Fira Sans",    sans-serif;  margin-bottom: 1rem;}textarea {  padding: 0.2rem;}label,br {  margin: 0.5rem 0;}button {  vertical-align: top;  height: 1.5rem;}
js
const channel = new BroadcastChannel("example-channel");const messageControl = document.querySelector("#message");const broadcastMessageButton = document.querySelector("#broadcast-message");broadcastMessageButton.addEventListener("click", () => {  channel.postMessage(messageControl.value);});

Receiver 1

<h1>Receiver 1</h1><div></div>
body {  border: 1px solid black;  padding: 0.5rem;  height: 100px;  font-family: "Fira Sans", sans-serif;}h1 {  font:    1.6em "Fira Sans",    sans-serif;  margin-bottom: 1rem;}
js
const channel = new BroadcastChannel("example-channel");channel.addEventListener("message", (event) => {  received.textContent = event.data;});

Receiver 2

<h1>Receiver 2</h1><div></div>
body {  border: 1px solid black;  padding: 0.5rem;  height: 100px;  font-family: "Fira Sans", sans-serif;}h1 {  font:    1.6em "Fira Sans",    sans-serif;  margin-bottom: 1rem;}
js
const channel = new BroadcastChannel("example-channel");channel.addEventListener("message", (event) => {  received.textContent = event.data;});

Result

Specifications

Specification
HTML
# event-message
HTML
# handler-broadcastchannel-onmessage

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp