WindowClient: visibilityState property

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.

Note: This feature is only available in Service Workers.

Thuộc tính chỉ đọc visibilityState của giao diện WindowClient 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", "visible", hoặc "prerender".

Giá trị

Một chuỗi (xem Document.visibilityState để biết các giá trị).

Ví dụ

js
event.waitUntil(
  clients
    .matchAll({
      type: "window",
    })
    .then((clientList) => {
      for (const client of clientList) {
        if (client.url === "/" && "focus" in client) {
          if (client.visibilityState === "hidden") return client.focus();
        }
      }
      if (clients.openWindow) {
        return clients.openWindow("/");
      }
    }),
);

Thông số kỹ thuật

Thông số kỹ thuật
Service Workers Nightly
# client-visibilitystate

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