Clients: openWindow() method

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.

The openWindow() method of the Clients interface creates a new top level browsing context and loads a given URL. If the calling script doesn't have permission to show popups, openWindow() will throw an InvalidAccessError.

In Firefox, the method is allowed to show popups only when called as the result of a notification click event.

In Chrome for Android, the method may instead open the URL in an existing browsing context provided by a standalone web app previously added to the user's home screen. As of recently, this also works on Chrome for Windows.

Syntax

js
openWindow(url)

Parameters

url

A string representing the URL of the client you want to open in the window. Generally this value must be a URL from the same origin as the calling script.

Return value

A Promise that resolves to a WindowClient object if the URL is from the same origin as the service worker or a null value otherwise.

Exceptions

InvalidAccessError DOMException

The promise is rejected with this exception if none of the windows in the app's origin have transient activation.

Security requirements

Examples

js
// Send notification to OS if applicable
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,
  );
}

// Notification click event listener
self.addEventListener("notificationclick", (e) => {
  // Close the notification popout
  e.notification.close();
  // Get all the Window clients
  e.waitUntil(
    clients.matchAll({ type: "window" }).then((clientsArr) => {
      const windowToFocus = clientsArr.find(
        (windowClient) => windowClient.url === e.notification.data.url,
      );
      if (windowToFocus) {
        // If a Window tab matching the targeted URL already exists, focus that;
        windowToFocus.focus();
      } else {
        // Otherwise, open a new tab to the applicable URL and focus it.
        clients
          .openWindow(e.notification.data.url)
          .then((windowClient) => (windowClient ? windowClient.focus() : null));
      }
    }),
  );
});

Specifications

Specification
Service Workers Nightly
# clients-openwindow

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
openWindow
Chrome – Full support
Chrome 40 (Release date: 2015-01-21)
footnote
footnote Before Chrome 43, this method could only open URLs on the same origin.
footnote Since Chrome 51, URLs may open inside an existing browsing context provided by a standalone web app.
Edge – Full support
Edge 17 (Release date: 2018-04-30)
footnote Full support
Firefox – Full support
Firefox 44 (Release date: 2016-01-26)
footnote Full support
Opera – Full support
Opera 38 (Release date: 2016-06-08)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 40 (Release date: 2015-01-21)
footnote
footnote Before Chrome Android 43, this method could only open URLs on the same origin.
footnote Since Chrome Android 51, URLs may open inside an existing browsing context provided by a standalone web app.
Firefox for Android – Full support
Firefox for Android 44 (Release date: 2016-01-26)
footnote Full support
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote
footnote Before Samsung Internet 4.0, this method could only open URLs on the same origin.
footnote Since Samsung Internet 5.0, URLs may open inside an existing browsing context provided by a standalone web app.
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
footnote
footnote Before WebView Android 43, this method could only open URLs on the same origin.
footnote Since WebView Android 51, URLs may open inside an existing browsing context provided by a standalone web app.
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.