WindowClient

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

* Some parts of this feature may have varying levels of support.

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

Client WindowClient

インスタンスメソッド

WindowClient には、親インターフェイスである Client から継承したメソッドがあります。

WindowClient.focus()

現在のクライアントにユーザー入力フォーカスを与えます。

WindowClient.navigate()

指定された URL を制御されたクライアントのページに読み込みます。

インスタンスプロパティ

WindowClient には、親インターフェイスである Client から継承したプロパティがあります。

WindowClient.focused 読取専用

論理値で、現在のクライアントにフォーカスがあるかどうかを示します。

WindowClient.visibilityState 読取専用

現在のクライアントの可視性を示します。 この値は、"hidden""visible""prerender" のいずれかです。

js
self.addEventListener("notificationclick", (event) => {
  console.log("On notification click: ", event.notification.tag);
  event.notification.close();

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

仕様書

Specification
Service Workers
# windowclient

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
WindowClient
ancestorOrigins
Experimental
focus
focused
navigate
visibilityState

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Experimental. Expect behavior to change in the future.
Has more compatibility info.

関連情報