Client: thuộc tính url

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 url của giao diện Client trả về URL của client service worker hiện tại.

Giá trị

Một chuỗi.

Ví dụ

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

  // Kiểm tra xem cửa sổ hiện tại đã được mở hay chưa
  // và đưa nó vào tiêu điểm nếu có
  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("/");
        }
      }),
  );
});

Thông số kỹ thuật

Specification
Service Workers Nightly
# client-url

Khả năng tương thích với trình duyệt