WindowClient

WindowClient interface of the ServiceWorker API represents the scope of a service worker client that is a document in a browsing context, controlled by an active worker. The service worker client independently selects and uses a service worker for its own loading and sub-resources.

方法

WindowClient inherits methods from its parent interface, Client .

WindowClient.focus()
Gives user input focus to the current client.
WindowClient.navigate()
Loads a specified URL into a controlled client page.

特性

WindowClient inherits properties from its parent interface, Client .

WindowClient.focused 只读
A boolean that indicates whether the current client has focus.
WindowClient.visibilityState 只读
Indicates the visibility of the current client. This value can be one of "hidden" , "visible" ,或 "prerender" .

范例

self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  event.notification.close();
  // This looks to see if the current is already open and
  // focuses if it is
  event.waitUntil(clients.matchAll({
    type: "window"
  }).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url == '/' && 'focus' in client) {
        client.focus();
        break;
      }
    }
    if (clients.openWindow)
      return clients.openWindow('/');
  }));
});
					

规范

规范 状态 Comment
服务工作者
The definition of 'WindowClient' in that specification.
工作草案 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
WindowClient
Chrome 42 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0
ancestorOrigins Chrome ? Edge ? Firefox ? IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android ? Firefox Android ? Opera Android ? Safari iOS 不支持 No Samsung Internet Android ?
focus
Chrome 42 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0
focused
Chrome 42 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0
navigate
Chrome 49 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 49 Firefox Android 44 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0
visibilityState
Chrome 42 Edge ≤18 Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera ? Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 44 Opera Android ? Safari iOS 不支持 No Samsung Internet Android 4.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。

另请参阅