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.

Note: This feature is only available in Service Workers.

Giao diện WindowClient của ServiceWorker API đại diện cho phạm vi của một service worker client là tài liệu trong ngữ cảnh duyệt web, được kiểm soát bởi một worker đang hoạt động. Service worker client độc lập chọn và sử dụng một service worker cho việc tải và tài nguyên phụ của chính nó.

Client WindowClient

Phương thức phiên bản

WindowClient kế thừa các phương thức từ giao diện cha, Client.

WindowClient.focus()

Đưa focus đầu vào của người dùng đến client hiện tại.

WindowClient.navigate()

Tải một URL được chỉ định vào trang client được kiểm soát.

Thuộc tính phiên bản

WindowClient kế thừa các thuộc tính từ giao diện cha, Client.

WindowClient.ancestorOrigins Read only Experimental

Một mảng các chuỗi cho biết nguồn gốc tổ tiên của ngữ cảnh duyệt web được đại diện bởi WindowClient này theo thứ tự ngược.

WindowClient.focused Read only

Một giá trị boolean cho biết liệu client hiện tại có focus hay không.

WindowClient.visibilityState Read only

Cho biết khả năng hiển thị của client hiện tại. Giá trị này có thể là một trong "hidden" hoặc "visible".

Ví dụ

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

  // This looks to see if the current is already open and
  // focuses if it is
  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("/");
      }),
  );
});

Thông số kỹ thuật

Specification
Service Workers Nightly
# windowclient

Tương thích trình duyệt

Xem thêm