aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/serviceworkercontainer/controller/index.html
blob: 349ba5758610ca2221fa11af620eafe51938483e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
title: ServiceWorkerContainer.controller
slug: Web/API/ServiceWorkerContainer/controller
translation_of: Web/API/ServiceWorkerContainer/controller
---
<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>

<p><span class="seoSummary">当状态为<code>activated</code> 时, {{domxref("ServiceWorkerContainer")}} 接口的只读属性 <strong><code>controller</code></strong> 返回一个 {{domxref("ServiceWorker")}} 对象(与 {{domxref("ServiceWorkerRegistration.active")}} 返回的对象是同一个)。当页面强制刷新 (<em>Shift</em> + refresh) 或不存在active worder时,该属性返回 <code>null</code> 。</span></p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var myController = navigator.serviceWorker.controller;
</pre>

<h3 id="值"></h3>

<p>一个{{domxref("ServiceWorker")}}对象.</p>

<h2 id="示例">示例</h2>

<pre class="brush: js">if ('serviceWorker' in navigator) {
  // Do a one-off check to see if a service worker's in control.
  if (navigator.serviceWorker.controller) {
    console.log('This page is currently controlled by:',
      navigator.serviceWorker.controller);
  } else {
    console.log('This page is not currently controlled ' +
      'by a service worker.');
  }
} else {
  console.log('Service workers are not supported.');
}</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">附注</th>
  </tr>
  <tr>
   <td>{{SpecName('Service Workers', '#navigator-service-worker-controller', 'ServiceWorkerRegistration.controller')}}</td>
   <td>{{Spec2('Service Workers')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>
<div class="hidden">此页面上的兼容性表从结构化数据生成。如果您想做出贡献,请查看 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并发送拉取请求.</div>

<p>{{Compat("api.ServiceWorkerContainer.controller")}}</p>
</div>