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

関連する問題