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

Clients.openWindow() - 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 ..

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

Firefox .

Chrome Android URL- , - (standalone web app), . Chrome Windows.

In this article

self.clients.openWindow(url).then(function(windowClient) {
  // Do something with your WindowClient
});

url

String - URL- , . URL , .

Promise, WindowClient, URL- , -, null value .

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();
  //    Windows
  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