简体   繁体   中英

How can I open the tab, the notification came from?

I want to use HTML5 notifications:

var n = new Notification("Hello");

How can I open the tab, the notification came from?

Handle the onclick event and call window.open .

n.onclick = function() { 
    window.open(yourUrl);
};

Add an event listener for the click event and then use window.focus . For example:

var n = new Notification("Hello");

n.addEventListener("click", function() {
    window.focus();
});

As far as I know, java script does not know if there are any other tabs open and surely not which websites you currently browse.

With a notification you can only open the tab where the notification came from, which should be the default behavior, if you click the notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM