ServiceWorkerContainer.controller

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

当状态为activated 时, ServiceWorkerContainer 接口的只读属性 controller 返回一个 ServiceWorker 对象(与 ServiceWorkerRegistration.active (en-US) 返回的对象是同一个)。当页面强制刷新 (Shift + refresh) 或不存在 active worder 时,该属性返回 null

语法

var myController = navigator.serviceWorker.controller;

一个ServiceWorker对象。

示例

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.");
}

规范

Specification
Service Workers
# navigator-service-worker-controller

浏览器兼容性

BCD tables only load in the browser