dom / latest / messageevent / messageevent.html /

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 MessageEvent that will be created. Some of the more common types are: message, messageerror, connect.

init Optional

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 to null if not specified.
  • origin: A USVString representing the origin of the message emitter. This defaults to an empty string ("") if not specified.
  • lastEventId: A DOMString representing a unique ID for the event. This defaults to an empty string ("") if not specified.
  • source: An MessageEventSource (which can be a WindowProxy, MessagePort, or ServiceWorker object) representing the message emitter. This defaults to null if not set.
  • ports: 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.

Examples

var myMessage = new MessageEvent('message', {
  data : 'hello'
});

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
MessageEvent
1
12
26
9
≤12.1
4
4.4
18
26
≤12.1
3.2
1.0

See also

  • ExtendableMessageEvent — similar to this interface but used in interfaces that needs to give more flexibility to authors.

© 2005–2021 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