WindowClient
インターフェイスの focus()
メソッドは、現在のクライアントにユーザー入力フォーカスを与え、既存の WindowClient
に解決する Promise
を返します。
構文
windowClient.focus().then(function(windowClient) { // WindowClient がフォーカスされたら、何かを行います });
パラメーター
なし。
戻り値
既存の WindowClient
に解決される Promise
。
例
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// これは、クライアントが既に開いているかどうかを確認し、
// 開いている場合にフォーカスを合わせます
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
});
仕様
仕様 | 状態 | コメント |
---|---|---|
Service Workers focus() の定義 |
草案 | 初期定義 |
ブラウザーの互換性
BCD tables only load in the browser
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.