MessageEvent

MessageEvent interface represents a message received by a target object.

This is used to represent messages in:

The action triggered by this event is defined in a function set as the event handler for the relevant message event (e.g. using an onmessage handler as listed above).

注意: 此特征可用于 Web 工作者 .

构造函数

MessageEvent()
创建新的 MessageEvent .

特性

This interface also inherits properties from its parent, Event .

MessageEvent.data 只读
The data sent by the message emitter.
MessageEvent.origin 只读
A USVString representing the origin of the message emitter.
MessageEvent.lastEventId 只读
A DOMString representing a unique ID for the event.
MessageEvent.source 只读
A MessageEventSource (which can be a WindowProxy , MessagePort ,或 ServiceWorker object) representing the message emitter.
MessageEvent.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 interface also inherits methods from its parent, Event .

initMessageEvent()
Initializes a message event. Do not use this anymore 使用 MessageEvent() constructor instead.

范例

In our Basic shared worker example ( run shared worker ), we have two HTML pages, each of which uses some JavaScript to perform a simple calculation. The different scripts are using the same worker file to perform the calculation — they can both access it, even if their pages are running inside different windows.

The following code snippet shows creation of a SharedWorker object using the SharedWorker() constructor. Both scripts contain this:

var myWorker = new SharedWorker('worker.js');
					

Both scripts then access the worker through a MessagePort object created using the SharedWorker.port property. If the onmessage event is attached using addEventListener, the port is manually started using its start() method:

myWorker.port.start();
					

When the port is started, both scripts post messages to the worker and handle messages sent from it using port.postMessage() and port.onmessage , respectively:

first.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}
second.onchange = function() {
  myWorker.port.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}
myWorker.port.onmessage = function(e) {
  result1.textContent = e.data;
  console.log('Message received from worker');
}
					

Inside the worker we use the SharedWorkerGlobalScope.onconnect handler to connect to the same port discussed above. The ports associated with that worker are accessible in the connect event's ports property — we then use MessagePort start() method to start the port, and the onmessage handler to deal with messages sent from the main threads.

onconnect = function(e) {
  var port = e.ports[0];
  port.addEventListener('message', function(e) {
    var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
    port.postMessage(workerResult);
  });
  port.start(); // Required when using addEventListener. Otherwise called implicitly by onmessage setter.
}
					

规范

规范 状态 Comment
HTML Living Standard
The definition of 'MessageEvent' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MessageEvent Chrome 1 Edge 12 Firefox 4 IE 9 Opera 10.6 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 3 Samsung Internet Android 1.0
MessageEvent() 构造函数 Chrome 1 Edge 12 Firefox 4 IE 9 Opera ? Safari 4 WebView Android 37 Chrome Android 18 Firefox Android ? Opera Android ? Safari iOS 3 Samsung Internet Android 1.0
data Chrome 1 Edge 12 Firefox 4 IE 9 Opera Yes Safari 4 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
initMessageEvent
弃用
Chrome 1 Edge 12 Firefox 4 IE 9 Opera Yes Safari 4 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
lastEventId Chrome 1 Edge 17 Firefox 4 IE 9 Opera Yes Safari 4 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
origin Chrome 1 Edge 12 Firefox 4 IE 9 Opera Yes Safari 4 WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
ports Chrome 1 Edge 12 Firefox 4 IE 9 Opera Yes Safari 4 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS 3 Samsung Internet Android Yes
source Chrome Yes Edge 12 Firefox 55 IE No Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 55 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
弃用。不要用于新网站。
弃用。不要用于新网站。

另请参阅

  • ExtendableMessageEvent — similar to this interface but used in interfaces that needs to give more flexibility to authors.
  1. MessageEvent
  2. 构造函数
    1. MessageEvent()
  3. 特性
    1. data
    2. lastEventId
    3. origin
    4. ports
    5. source
  4. 继承:
    1. Event
  5. Related pages for HTML DOM
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. History
    81. ImageData
    82. Location
    83. MessageChannel
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers