Client: type プロパティ
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
type
は Client
インターフェイスの読み取り専用プロパティで、サービスワーカーが制御しているクライアントの種類を示します。
値
クライアントの種類を表す文字列。 値は次のいずれかです。
"window"
"worker"
"sharedworker"
例
js
// サービスワーカークライアント(文書など)
function sendMessage(message) {
return new Promise((resolve, reject) => {
// これは ServiceWorker.postMessage バージョンであることに注意してください
navigator.serviceWorker.controller.postMessage(message);
window.serviceWorker.onMessage = (e) => {
resolve(e.data);
};
});
}
// 制御するサービスワーカー
self.addEventListener("message", (e) => {
// e.source はクライアントオブジェクトです
e.source.postMessage(`こんにちは! あなたのメッセージは: ${e.data}`);
// type 値も投稿してクライアントに戻しましょう
e.source.postMessage(e.source.type);
});
仕様書
Specification |
---|
Service Workers # client-type |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
type |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.