---
title: ServiceWorkerContainer.onmessage
slug: Web/API/ServiceWorkerContainer/onmessage
tags:
  - API
  - Property
  - Reference
  - Service Workers
  - ServiceWorker
  - ServiceWorkerContainer
translation_of: Web/API/ServiceWorkerContainer/onmessage
---
<p>{{APIRef("Service Workers API")}}{{ SeeCompatTable() }}</p>

<p><span class="seoSummary">{{domxref("ServiceWorkerContainer")}} インターフェイスの <strong><code>onmessage</code></strong> プロパティは、{{Event("message")}} イベントが発生するたびに発火するイベントハンドラーです。 (例えば、{{domxref("Client.postMessage()")}} 呼び出しを介して)着信メッセージを {{domxref("ServiceWorkerContainer")}} オブジェクトに受信したときに発生します。</span></p>

<div class="note">
<p><strong>注</strong>: 他のウェブメッセージング機能との整合性を保つため、サービスワーカーコンテキストから受信されるメッセージ(<code>onmessage</code> のイベントオブジェクトなど)は、最新のブラウザーでは {{domxref("MessageEvent")}} オブジェクトによって表されます。 (これらは、以前は廃止された {{domxref("ServiceWorkerMessageEvent")}} オブジェクトで表されていました。)</p>
</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox"><em>serviceWorkerContainer</em>.onmessage = function(<em>messageevent</em>) { ... }</pre>

<h2 id="Example" name="Example">例</h2>

<pre class="brush: js">navigator.serviceWorker.onmessage = function(messageevent) {
  console.log(`received data: ${messageevent.data}`);
}
</pre>

<h2 id="Specifications" name="Specifications">仕様</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">仕様</th>
   <th scope="col">状態</th>
   <th scope="col">コメント</th>
  </tr>
  <tr>
   <td>{{SpecName('Service Workers', '#dom-serviceworkerglobalscope-onmessage', 'ServiceWorkerContainer: onmessage')}}</td>
   <td>{{Spec2('Service Workers')}}</td>
   <td>初期定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div>


<p>{{Compat("api.ServiceWorkerContainer.onmessage")}}</p>
</div>