ServiceWorkerContainer.controller

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

当状态为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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
controller
Experimental
Chrome Full support 40Edge Full support 17
Full support 17
Full support 16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Firefox Android Full support 44Opera Android Full support 27Safari iOS Full support 11.3Samsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.