function sendNotification (title = 'Hi') {
var a = new Notification(title, {
requireInteraction: true,
renotify: true,
tag: 123,
})
a.onclick = function () { // Click event will lose after refresh
console.log('get')
}
}
if (!("Notification" in window)) {
alert("not support");
}
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
sendNotification()
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission()
.then(
permission => {
if (permission === "granted") {
sendNotification()
}
}
)
}
以上代码将创建通知并保持通知不会消失。当我点击通知时会记录123,但刷新页面后我丢失了点击事件和变量a的通知。
为了方便测试,我添加了身份验证。您可以将代码复制到浏览器控制台调试。
答案 0 :(得分:0)
请尝试以下代码:
document.onload = function(){
a.onclick = function () {
console.log(123)
}
};