--- title: Clients.openWindow() slug: Web/API/Clients/openWindow translation_of: Web/API/Clients/openWindow ---
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
{{domxref("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// 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 |
|---|---|---|
| {{SpecName('Service Workers', '#clients', 'Clients')}} | {{Spec2('Service Workers')}} | Initial definition. |
{{Compat("api.Clients.openWindow")}}