ServiceWorkerContainer: controller 属性
注意: 此功能在 Web Workers 中可用。
controller
是 ServiceWorkerContainer
接口的只读属性,如果其状态为 activating
或 activated
(与 ServiceWorkerRegistration.active
返回的相同对象),则返回一个 ServiceWorker
对象。如果请求是强制刷新(Shift + 刷新)或没有活动的 worker,则此属性返回 null
。
值
一个 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.");
}
规范
规范 |
---|
Service Workers # navigator-service-worker-controller |
浏览器兼容性
BCD 表仅在启用 JavaScript 的浏览器中加载。