How to get focus to a Chrome tab which created desktop notification?

后端 未结 5 1769
北荒
北荒 2020-12-07 14:50

I would like to implement same functionality as Gmail has nowadays. When new email arrives or new chat comes, notification popup appears and if you click it, the tab with Gm

5条回答
  •  一个人的身影
    2020-12-07 15:13

    It's not really good practice to use the onclick property, use the addEventListener for vanilla javascript or on method for jQuery.

    var notify = new Notification('Test notification');
    

    Vanilla:

    notify.addEventListener('click', function(e) {
        window.focus();
        e.target.close();
    }, false);
    

    jQuery:

    $(notify).on('click', function(e) {
        window.focus();
        e.target.close();
    });
    

提交回复
热议问题