-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.md
174 lines (132 loc) · 4.12 KB
/
index.md
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: "BroadcastChannel: message event"
short-title: message
slug: Web/API/BroadcastChannel/message_event
page-type: web-api-event
browser-compat: api.BroadcastChannel.message_event
---
{{APIRef("BroadCastChannel API")}}{{AvailableInWorkers}}
The **`message`** event of the {{domxref("BroadcastChannel")}} interface fires when a message arrives on that channel.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js-nolint
addEventListener("message", (event) => { })
onmessage = (event) => { }
```
## Event type
A {{domxref("MessageEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("MessageEvent")}}
## Event properties
_In addition to the properties listed below, properties from the parent interface, {{domxref("Event")}}, are available._
- {{domxref("MessageEvent.data", "data")}} {{ReadOnlyInline}}
- : The data sent by the message emitter.
- {{domxref("MessageEvent.origin", "origin")}} {{ReadOnlyInline}}
- : A string representing the origin of the message emitter.
- {{domxref("MessageEvent.lastEventId", "lastEventId")}} {{ReadOnlyInline}}
- : A string representing a unique ID for the event.
- {{domxref("MessageEvent.source", "source")}} {{ReadOnlyInline}}
- : A _message event source_, which is either a {{glossary("WindowProxy")}}, a {{domxref("MessagePort")}}, or a {{domxref("ServiceWorker")}} object representing the message emitter.
- {{domxref("MessageEvent.ports", "ports")}} {{ReadOnlyInline}}
- : An array of {{domxref("MessagePort")}} 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" {{HTMLElement("iframe")}} that broadcasts the contents of a {{HTMLElement("textarea")}} when the user clicks a button. There are two "receiver" iframes that listen to the broadcast message and write the result into a {{HTMLElement("div")}} element.
### Sender
```html hidden
<h1>Sender</h1>
<label for="message">Type a message to broadcast:</label><br />
<textarea id="message" name="message" rows="1" cols="40">Hello</textarea>
<button id="broadcast-message" type="button">Broadcast message</button>
```
```css hidden
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
```html hidden
<h1>Receiver 1</h1>
<div id="received"></div>
```
```css hidden
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
```html hidden
<h1>Receiver 2</h1>
<div id="received"></div>
```
```css hidden
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
{{ EmbedLiveSample('Sender', '100%', 220) }}
{{ EmbedLiveSample('Receiver_1', '100%', 160) }}
{{ EmbedLiveSample('Receiver_2', '100%', 160) }}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- Related events: {{domxref("BroadcastChannel/messageerror_event", "messageerror")}}.