openWindow()
方法在
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.
self.clients.openWindow(url).then(function(windowClient) {
// Do something with your WindowClient
});
url
USVString
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.
Promise
解析为
WindowClient
object if the URL is from the same origin as the service worker or a
null value
否则。
// 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 => {
// If a Window tab matching the targeted URL already exists, focus that;
const hadWindowToFocus = clientsArr.some(windowClient => windowClient.url === e.notification.data.url ? (windowClient.focus(), true) : false);
// Otherwise, open a new tab to the applicable URL and focus it.
if (!hadWindowToFocus) clients.openWindow(e.notification.data.url).then(windowClient => windowClient ? windowClient.focus() : null);
}));
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
服务工作者
The definition of 'Clients: openWindow' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
openWindow
|
Chrome
40
|
Edge
≤79
|
Firefox
45
注意事项
|
IE 不支持 No | Opera 38 | Safari 不支持 No |
WebView Android
40
|
Chrome Android
40
|
Firefox Android 45 | Opera Android 41 | Safari iOS 不支持 No |
Samsung Internet Android
4.0
|
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
Clients
claim()
get()
matchAll()
openWindow()
缓存
CacheStorage
Client
ExtendableEvent
FetchEvent
InstallEvent
Navigator.serviceWorker
NotificationEvent
PeriodicSyncEvent
PeriodicSyncManager
PeriodicSyncRegistration
ServiceWorker
ServiceWorkerContainer
ServiceWorkerGlobalScope
ServiceWorkerRegistration
SyncEvent
SyncManager
SyncRegistration
WindowClient