2017-06-17 85 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

相關問題