Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. MessageEvent
  4. MessageEvent()

MessageEvent: MessageEvent() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨August 2016⁩.

Note: This feature is available inWeb Workers.

TheMessageEvent() constructor creates a newMessageEvent object.

Syntax

js
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 tomessage.

optionsOptional

An object that,in addition of the properties defined inEvent(), can have the following properties:

dataOptional

The data you want contained in the MessageEvent.This can be of any data type, and will default tonull if 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

AMessageEventSource (which can be aWindow, aMessagePort,or aServiceWorker object) representing the message emitter.This defaults tonull if not set.

portsOptional

An array ofMessagePort objects containing allMessagePort objects sent with the message, in order.This defaults to an empty array ([]) if not specified.

Return value

A newMessageEvent object.

Examples

js
const myMessage = new MessageEvent("message", {  data: "hello",});

Specifications

Specification
HTML
# the-messageevent-interface:dom-event-constructor

Browser compatibility

See also

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

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp