Clients.openWindow()

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

Clients接口的 openWindow() 方法创建一个顶级的浏览器上下文并加载给定的 URL. 如果调用脚本没有显示弹出窗口的权限, openWindow() 将抛出 InvalidAccessError.

在Firefox中,只有在作为通知点击事件的结果调用时,才允许该方法显示弹出窗口.

在Chrome for Android中,该方法可以改为在先前添加到用户主屏幕的 standalone web app 提供的现有浏览上下文中打开URL.

语法

ServiceWorkerClients.openWindow(url).then(function(WindowClient) {
  // do something with your WindowClient
});

参数

url
一个 USVString ,表示要在窗口中打开的client的URL。 通常,此值必须是与调用脚本有相同域的URL.

返回值

如果URL来自与服务工作者相同的域,则resolve为 WindowClient 对象的Promise,否则resolve为 null value .

示例

// When the user clicks a notification focus the window if it exists or open
// a new one otherwise.
onotificationclick = function(event) {
  var found = false;
  clients.matchAll().then(function(clientsArr) {
    for (i = 0; i < clientsArr.length; i++) {
      if (clientsArr[i].url === event.data.url) {
        // We already have a window to use, focus it.
        found = true;
        clientsArr[i].focus();
        break;
      }
    }
    if (!found) {
      // Create a new window.
      clients.openWindow(event.data.url).then(function(windowClient) {
        // do something with the windowClient.
      });
    }
  });
};

规范

Specification Status Comment
Service Workers
Clients
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
Experimental
Chrome Full support 40
Full support 40
Full support 42
Notes
Notes Can only open URLs on the same origin.
Full support 43
Notes
Notes Can open any URL.
Full support 51
Notes
Notes URLs may open inside an existing browsing context provided by a standalone web app
Edge ? Firefox Full support 45
Notes
Full support 45
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR).
IE No support NoOpera Full support 38Safari No support NoWebView Android Full support 40
Full support 40
Full support 42
Notes
Notes Can only open URLs on the same origin.
Full support 43
Notes
Notes Can open any URL.
Full support 51
Notes
Notes URLs may open inside an existing browsing context provided by a standalone web app
Chrome Android Full support 40
Full support 40
Full support 42
Notes
Notes Can only open URLs on the same origin.
Full support 43
Notes
Notes Can open any URL.
Full support 51
Notes
Notes URLs may open inside an existing browsing context provided by a standalone web app
Edge Mobile ? Firefox Android Full support 45Opera Android Full support 38Safari iOS No support NoSamsung Internet Android Full support 4.0
Full support 4.0
Full support 5.0
Notes
Notes URLs may open inside an existing browsing context provided by a standalone web app

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

文档标签和贡献者

此页面的贡献者: flyingsouthwind
最后编辑者: flyingsouthwind,