function spawnNotification(theBody, theIcon, theTitle, theLink) {
var options = {
body: theBody,
icon: theIcon
}
var notification = new Notification(theTitle, options);
notification.onclick = function() {
var myWindow = window.open(theLink,"_blank");
myWindow.focus();
};
setTimeout(notification.close.bind(notification), 4000);
}
我在点击通知框时尝试打开新标签并对其进行对焦。
我正在使用上述功能在新标签页中打开链接,并专注于新打开的标签页。但它不起作用。
新标签页面已打开,但焦点仍保留在旧标签页上。我该如何解决这个问题?
答案 0 :(得分:4)
function spawnNotification(theBody, theIcon, theTitle, theLink) {
var options = {
body: theBody,
icon: theIcon
}
var notification = new Notification(theTitle, options);
notification.onclick = function(event) {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open(theLink, '_blank');
}
setTimeout(notification.close.bind(notification), 7000);
}
我改变了我的代码,如上所示。现在它完美无缺。 请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Notification/onclick#Examples