javascript - Switch to the tab that sent the notification when the notification is clicked? - Stack Overflow

Let's say I have a site and it sends a notification in the background while the tab is still open.

Let's say I have a site and it sends a notification in the background while the tab is still open. How do I have the browser switch to my tab if the user clicks on my notification using JavaScript (pure, no jQuery)?

Let's say I have a site and it sends a notification in the background while the tab is still open. How do I have the browser switch to my tab if the user clicks on my notification using JavaScript (pure, no jQuery)?

Share asked Apr 5, 2018 at 19:44 herronjoherronjo 951 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

I'm assuming you are using the Notifications API. In your code, after you create the notification, you'll want to add an onclick event and tell it to focus on the tab (rather than the browser).

let notification = new Notification('Test Notification'});
notification.onclick = function () { 
    window.parent.parent.focus();
};

According to the documentation, you can also use the addEventListener() method instead of onclick if you like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744935863a4602019.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信