WindowClient: focus() 方法
注意:此功能仅在 Service Workers 中可用。
WindowClient 接口的 focus() 方法将用户输入焦点赋予当前客户端,并返回一个 Promise,该 Promise 解析为现有的 WindowClient。
语法
js
focus()
参数
无。
返回值
一个 Promise,解析为现有的 WindowClient。
异常
InvalidAccessErrorDOMException-
如果应用程序源中的任何窗口都没有瞬时激活,则 Promise 将被此异常拒绝。
安全要求
- 应用程序源中至少有一个窗口必须具有瞬时激活。
示例
js
self.addEventListener("notificationclick", (event) => {
console.log("On notification click: ", event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
type: "window",
})
.then((clientList) => {
for (const client of clientList) {
if (client.url === "/" && "focus" in client) return client.focus();
}
if (clients.openWindow) return clients.openWindow("/");
}),
);
});
规范
| 规范 |
|---|
| Service Workers # client-focus |
浏览器兼容性
加载中…