Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. ServiceWorkerGlobalScope
  4. messageerror

ServiceWorkerGlobalScope: messageerror event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.

Note: This feature is only available inService Workers.

Themessageerror event of theServiceWorkerGlobalScope interface occurs when incoming messages can't be deserialized.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods likeaddEventListener(), or set an event handler property.

js
addEventListener("messageerror", (event) => { })onmessageerror = (event) => { }

Event type

AnExtendableMessageEvent. Inherits fromExtendableEvent.

Event ExtendableEvent ExtendableMessageEvent

Event properties

Inherits properties from its parent,ExtendableEvent.

ExtendableMessageEvent.dataRead only

Returns the event's data. It can be any data type. If dispatched inmessageerror event, the property will benull.

ExtendableMessageEvent.originRead only

Returns the origin of theClient that sent the message.

ExtendableMessageEvent.lastEventIdRead only

Represents, inserver-sent events, the last event ID of the event source.

ExtendableMessageEvent.sourceRead only

Returns a reference to theClient object that sent the message.

ExtendableMessageEvent.portsRead only

Returns the array containing theMessagePort objects representing the ports of the associated message channel.

Examples

In the below example a page gets a handle to theServiceWorker object viaServiceWorkerRegistration.active, and then calls itspostMessage() function.

js
// main.jsif (navigator.serviceWorker) {  navigator.serviceWorker.register("service-worker.js");  navigator.serviceWorker.addEventListener("message", (event) => {    // event is a MessageEvent object    console.log(`The service worker sent me a message: ${event.data}`);  });  navigator.serviceWorker.ready.then((registration) => {    registration.active.postMessage("Hi service worker");  });}

The service worker can listen for the message deserialization error by listening to themessageerror event:

js
// service-worker.jsself.addEventListener("messageerror", (event) => {  // event is an ExtendableMessageEvent object  console.error("Message deserialization failed");});

Alternatively, the script can listen for the message deserialization error usingonmessageerror:

js
// service-worker.jsself.onmessageerror = (event) => {  // event is an ExtendableMessageEvent object  console.error("Message deserialization failed");};

Specifications

Specification
Service Workers Nightly
# eventdef-serviceworkerglobalscope-messageerror
Service Workers Nightly
# dom-serviceworkerglobalscope-onmessageerror

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp