[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/Clients/openWindow [Back]  [Original]

Clients: openWindow() - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Clients: openWindow()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2018 4.

: This feature is only available in Service Workers.

Clients openWindow() URL . , openWindow() InvalidAccessError .

Firefox .

Android Chrome, URL . Windows Chrome .

In this article

js
openWindow(url)

url

URL . URL .

URL WindowClient , null Promise.

InvalidAccessError DOMException

.

js
//   OS  
if (self.Notification.permission === "granted") {
  const notificationObject = {
    body: "Click here to view your messages.",
    data: { url: `${self.location.origin}/some/path` },
    // data: { url: 'http://example.com' },
  };
  self.registration.showNotification(
    "You've got messages!",
    notificationObject,
  );
}

//    
self.addEventListener("notificationclick", (e) => {
  //   
  e.notification.close();
  //  Window  
  e.waitUntil(
    clients.matchAll({ type: "window" }).then((clientsArr) => {
      //  URL     ,   
      const hadWindowToFocus = clientsArr.some((windowClient) =>
        windowClient.url === e.notification.data.url
          ? (windowClient.focus(), true)
          : false,
      );
      //   ,  URL    .
      if (!hadWindowToFocus)
        clients
          .openWindow(e.notification.data.url)
          .then((windowClient) => (windowClient ? windowClient.focus() : null));
    }),
  );
});

Specification
Service Workers Nightly
# clients-openwindow


Web Proxy Viewer  |  New URL  |  Original Page