客户端:类型属性
注意:此功能仅在 Service Workers 中可用。
type
是 Client
接口的只读属性,它指示 Service Worker 控制的客户端类型。
值
一个字符串,表示客户端类型。该值可以是以下之一:
"window"
"worker"
"sharedworker"
示例
js
// service worker client (e.g. a document)
function sendMessage(message) {
return new Promise((resolve, reject) => {
// note that this is the ServiceWorker.postMessage version
navigator.serviceWorker.controller.postMessage(message);
window.serviceWorker.onMessage = (e) => {
resolve(e.data);
};
});
}
// controlling service worker
self.addEventListener("message", (e) => {
// e.source is a client object
e.source.postMessage(`Hello! Your message was: ${e.data}`);
// Let's also post the type value back to the client
e.source.postMessage(e.source.type);
});
规范
规范 |
---|
Service Workers # client-type |
浏览器兼容性
BCD 表格仅在浏览器中加载