MessageEvent()
The MessageEvent() constructor creates a new MessageEvent object instance.
Syntax
new MessageEvent(type)
new MessageEvent(type, init)
Parameters
type-
A string indicating the type of
MessageEventthat will be created. Some of the more common types are:message,messageerror,connect. initOptional-
A dictionary object that can contain the following properties:
-
data: The data you want contained in the MessageEvent. This can be of any data type, and will default tonullif not specified. -
origin: AUSVStringrepresenting the origin of the message emitter. This defaults to an empty string ("") if not specified. -
lastEventId: ADOMStringrepresenting a unique ID for the event. This defaults to an empty string ("") if not specified. -
source: AnMessageEventSource(which can be aWindowProxy,MessagePort, orServiceWorkerobject) representing the message emitter. This defaults tonullif not set. -
ports: An array ofMessagePortobjects 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.
-
Example
var myMessage = new MessageEvent('message', {
data : 'hello'
});
Specifications
| Specification |
|---|
| HTML Standard # the-messageevent-interface:dom-event-constructor |
Browser compatibility
BCD tables only load in the browser
See also
ExtendableMessageEvent— similar to this interface but used in interfaces that needs to give more flexibility to authors.