Clients interface provides access to Client objects. Access it via self .clients service worker .

方法

Clients.get()
Returns a Promise 对于 Client matching a given id .
Clients.matchAll()
返回 Promise for an array of Client objects. An options argument allows you to control the types of clients returned.
Clients.openWindow()
Opens a new browser window for a given url and returns a Promise for the new WindowClient .
Clients.claim()
Allows an active service worker to set itself as the controller for all clients within its scope .

范例

The following example shows an existing chat window or creates a new one when the user clicks a notification.

addEventListener('notificationclick', event => {
  event.waitUntil(async function() {
    const allClients = await clients.matchAll({
      includeUncontrolled: true
    });
    let chatClient;
    // Let's see if we already have a chat window open:
    for (const client of allClients) {
      const url = new URL(client.url);
      if (url.pathname == '/chat/') {
        // Excellent, let's use it!
        client.focus();
        chatClient = client;
        break;
      }
    }
    // If we didn't find an existing chat window,
    // open a new one:
    if (!chatClient) {
      chatClient = await clients.openWindow('/chat/');
    }
    // Message the client:
    chatClient.postMessage("New chat messages!");
  }());
});
					

规范

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

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Clients Chrome 40 Edge ≤79 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 不支持 No WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 不支持 No Samsung Internet Android 4.0
claim Chrome 42 Edge ≤79 Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 42 Chrome Android 42 Firefox Android 44 Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
get Chrome 51 Edge ≤79 Firefox 45 注意事项
45 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 38 Safari 不支持 No WebView Android 不支持 No Chrome Android 51 Firefox Android 45 Opera Android 41 Safari iOS 不支持 No Samsung Internet Android 5.0
matchAll Chrome 47 注意事项
47 注意事项
Client objects returned in most recent focus order.
Edge ≤79 注意事项
≤79 注意事项
Client objects returned in most recent focus order.
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
54 注意事项
Client objects returned in most recent focus order.
IE 不支持 No Opera 32 Safari 不支持 No WebView Android 47 注意事项
47 注意事项
Client objects returned in most recent focus order.
Chrome Android 47 注意事项
47 注意事项
Client objects returned in most recent focus order.
Firefox Android 44
44
54 注意事项
Client objects returned in most recent focus order.
Opera Android 32 Safari iOS 不支持 No Samsung Internet Android 4.0 注意事项
4.0 注意事项
Client objects returned in most recent focus order.
openWindow Chrome 40
40
42 注意事项
Can only open URLs on the same origin.
43 注意事项
Can open any URL.
51 注意事项
URLs may open inside an existing browsing context provided by a standalone web app
Edge ≤79
≤79
≤79 注意事项
Can only open URLs on the same origin.
≤79 注意事项
Can open any URL.
≤79 注意事项
URLs may open inside an existing browsing context provided by a standalone web app
Firefox 45 注意事项
45 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 38 Safari 不支持 No WebView Android 40
40
42 注意事项
Can only open URLs on the same origin.
43 注意事项
Can open any URL.
51 注意事项
URLs may open inside an existing browsing context provided by a standalone web app
Chrome Android 40
40
42 注意事项
Can only open URLs on the same origin.
43 注意事项
Can open any URL.
51 注意事项
URLs may open inside an existing browsing context provided by a standalone web app
Firefox Android 45 Opera Android 41 Safari iOS 不支持 No Samsung Internet Android 4.0
4.0
5.0 注意事项
URLs may open inside an existing browsing context provided by a standalone web app

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改: