How to get focus to the tab when a desktop notification is clicked in Firefox?

主宰稳场 提交于 2019-12-08 15:18:22

问题


I am implementing desktop notifications in a website which displays notifications when new messages arrive. I want the browser tab in which the site is open to get focus when the user clicks on the notification & I could get this worked in Chrome with the following code:

var n = new Notification('Title', {
    'body': 'Sample content.'
});

n.onclick = function (e) {
    window.focus();
};

But unfortunately, this doesn't work in Firefox. :( Could anyone tell me what I am missing here? I am testing in Chrome 31 & Firefox 26

Thanks.


回答1:


In Firefox focusing window from JS is disabled due to security reasons. You have to switch flag dom.disable_window_flip to false in about:config. But by default it is disabled. The interesting moment is that in Chrome focusing window is disabled too except in response to user actions (such as click) and that's only reason why clicking on notifications works in Chrome.



来源:https://stackoverflow.com/questions/21016999/how-to-get-focus-to-the-tab-when-a-desktop-notification-is-clicked-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!