The MessageEvent()
constructor creates a new MessageEvent
object.
The MessageEvent()
constructor creates a new MessageEvent
object.
js
new MessageEvent(type) new MessageEvent(type, options)
type
A string with the name of the event. It is case-sensitive and browsers always set it to message
.
options
Optional
An object that, in addition of the properties defined in Event()
, can have the following properties:
data
Optional
The data you want contained in the MessageEvent. This can be of any data type, and will default to null
if not specified.
origin
Optional
A string representing the origin of the message emitter. This defaults to an empty string (''
) if not specified.
lastEventId
Optional
A string representing a unique ID for the event. This defaults to an empty string ("") if not specified.
source
Optional
A MessageEventSource
(which can be a Window
, a MessagePort
, or a ServiceWorker
object) representing the message emitter. This defaults to null
if not set.
ports
Optional
An array of 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). This defaults to an empty array ([]
) if not specified.
A new MessageEvent
object.
js
const myMessage = new MessageEvent("message", { data: "hello", });
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
MessageEvent |
16 | 14 | 26 | No | ≤12.1 | 6 | 4.4 | 18 | 26 | ≤12.1 | 6 | 1.0 |
ExtendableMessageEvent
— similar to this interface but used in interfaces that needs to give more flexibility to authors.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent