客户端:url 属性

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

urlClient 接口的只读属性,它返回当前服务工作者客户端的 URL。

价值

一个字符串。

示例

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) {
            return client.focus();
          }
        }
        if (clients.openWindow) {
          return clients.openWindow("/");
        }
      }),
  );
});

规范

规范
Service Workers
# client-url

浏览器兼容性

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