WindowClient:focused 属性

注意: 此功能仅在 Service Workers 中可用。

focusedWindowClient 接口的一个只读属性,它是一个布尔值,指示当前客户端是否具有焦点。

一个布尔值。

示例

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) {
            if (!client.focused) return client.focus();
          }
        }

        if (clients.openWindow) return clients.openWindow("/");
      }),
  );
});

规范

规范
Service Workers
# client-focused

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。