--- title: WindowClient slug: Web/API/WindowClient tags: - API - Client - Interface - Reference - Service Workers - ServiceWorker - WindowClient translation_of: Web/API/WindowClient ---

{{APIRef("Service Workers API")}}

Service Worker APIWindowClient インターフェイスは、アクティブなワーカーによって制御される閲覧コンテキスト内のドキュメントであるサービスワーカークライアントのスコープを表します。 サービスワーカークライアントは、それ自身の読み込みとサブリソースに対してサービスワーカーを個別に選択して使用します。

メソッド

WindowClient は、親インターフェースである {{domxref("Client")}} からメソッドを継承します。

{{domxref("WindowClient.focus()")}}
現在のクライアントにユーザー入力フォーカスを与えます。
{{domxref("WindowClient.navigate()")}}
指定された URL を制御されたクライアントのページに読み込みます。

プロパティ

WindowClient は、親インターフェースである {{domxref("Client")}} からプロパティを継承します。

{{domxref("WindowClient.focused")}} {{readonlyInline}}
現在のクライアントにフォーカスがあるかどうかを示すブール値。
{{domxref("WindowClient.visibilityState")}} {{readonlyInline}}
現在のクライアントの可視性を示します。 この値は、"hidden""visible""prerender" のいずれかです。

self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  event.notification.close();

  // これは、クライアントが既に開いているかどうかを確認し、
  // 開いている場合にフォーカスを合わせます
  event.waitUntil(clients.matchAll({
    type: "window"
  }).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url == '/' && 'focus' in client) {
        client.focus();
        break;
      }
    }
    if (clients.openWindow)
      return clients.openWindow('/');
  }));
});

仕様

仕様 状態 コメント
{{SpecName('Service Workers', '#windowclient', 'WindowClient')}} {{Spec2('Service Workers')}} 初期定義

ブラウザーの互換性

{{Compat("api.WindowClient")}}

関連情報