15

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.

Xmindz
  • 1,282
  • 13
  • 34

1 Answers1

13

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.

Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31
  • 1
    Thats just an explanation of why it doesn't work. I was looking for a solution :( – Xmindz Feb 27 '14 at 10:09
  • I do not pretend that this is the ultimate truth, but my assumption is that this problem has no solution other than setting flags in users config (which is usually not acceptable) or influence on the Mozilla Foundation to remove this security setting like it done in Chrome – Roman Bekkiev Feb 28 '14 at 08:57
  • 1
    Now focusing the tab is the default behavior in Firefox, even if you don't use `window.focus()`. – Marco Castelluccio Feb 13 '16 at 00:13