2017-06-17 75 views
2
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); 
} 

我试图打开一个新选项卡并将其集中,当通知框被单击时。通知onclick - 打开新标签中的链接并重点

我正在使用上述功能在新选项卡中打开链接并专注于新打开的选项卡。但它不工作。

新标签打开,但焦点仍然在旧标签本身。我该如何解决这个问题?

+0

'window.open(url,'_blank')'打开一个新选项卡并将其置于默认焦点,因此必须有其他内容导致焦点保留在原始选项卡中。 –

+0

@MathiasW我将编辑这个问题,并添加更多的我的代码。所以你可以帮助我。 – Dexter

回答

1
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

相关问题