--- title: Clients.claim() slug: Web/API/Clients/claim translation_of: Web/API/Clients/claim ---
{{SeeCompatTable}}{{APIRef("Service Worker Clients")}}
{{domxref("Clients")}} 接口的 claim()
方法允许一个激活的 service worker 将自己设置为其 {{domxref("ServiceWorkerRegistration.scope", "scope")}} 内所有clients 的{{domxref("ServiceWorkerContainer.controller", "controller")}} . 这会在由此service worker 控制的任何 clients 中触发 {{domxref("ServiceWorkerContainer","navigator.serviceWorker")}} 上的 "controllerchange
" 事件.
当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 claim()
方法会立即控制这些页面。请注意,这会导致 service worker 控制通过网络定期加载的页面,或者可能通过不同的 service worker 加载.
await clients.claim();
None.
A {{jsxref("Promise")}} for void
.
The following example uses claim()
inside service worker's "activate
" event listener so that clients loaded in the same scope do not need to be reloaded before their fetches will go through this service worker.
self.addEventListener('activate', event => { event.waitUntil(clients.claim()); });
Specification | Status | Comment |
---|---|---|
{{SpecName('Service Workers', '#clients-claim', 'claim()')}} | {{Spec2('Service Workers')}} | Initial definition. |
{{Compat("api.Clients.claim")}}