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);
}
当单击通知框时,我正在尝试打开一个新选项卡并将其聚焦。
我正在使用上述功能在新选项卡中打开链接并专注于新打开的选项卡。但它不起作用。
新选项卡已打开,但焦点仍位于旧选项卡本身。我该如何解决这个问题?
最佳答案
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
关于javascript - 通知 onclick - 在新选项卡中打开链接并关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603081/