MessageEvent: MessageEvent() constructor
The MessageEvent() constructor creates a new MessageEvent object.
Syntax
new MessageEvent(type)
new MessageEvent(type, options)
Parameters
type-
A string with the name of the event. It is case-sensitive and browsers always set it to
message. optionsOptional-
An object that, in addition of the properties defined in
Event(), can have the following properties:dataOptional-
The data you want contained in the MessageEvent. This can be of any data type, and will default to
nullif not specified. originOptional-
A string representing the origin of the message emitter. This defaults to an empty string (
'') if not specified. lastEventIdOptional-
A string representing a unique ID for the event. This defaults to an empty string ("") if not specified.
sourceOptional-
A
MessageEventSource(which can be aWindow, aMessagePort, or aServiceWorkerobject) representing the message emitter. This defaults tonullif not set. portsOptional-
An array of
MessagePortobjects 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.
Return value
A new MessageEvent object.
Examples
const myMessage = new MessageEvent("message", {
data: "hello",
});
Specifications
| Specification |
|---|
| HTML Standard # the-messageevent-interface:dom-event-constructor |
Browser compatibility
| desktop | mobile | server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
MessageEvent() constructor | |||||||||||||
See also
ExtendableMessageEvent— similar to this interface but used in interfaces that needs to give more flexibility to authors.