ServiceWorkerContainer.controller

Experimental: 这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

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

语法

var myController = navigator.serviceWorker.controller;

一个ServiceWorker对象.

示例

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

规范

规范 状态 附注
Service Workers
ServiceWorkerRegistration.controller
Working Draft Initial definition

浏览器兼容性

BCD tables only load in the browser