7

My page should make notification sounds only when the window is in the background. I can track the window.onfocus and window.onblur events to notice when the focus state changes. However, I don't know whether the window will load focused or not, since it might load in a background tab, for instance.

How do I decide whether to play sounds or not before I get an onfocus/onblur event?

Peeja
  • 13,683
  • 11
  • 58
  • 77

2 Answers2

2

Couldn't you just assume it's loaded blurred, and then change the status to focused as soon as you receive any type of event (keydown/mousemove)?

Jed Schmidt
  • 2,907
  • 23
  • 23
  • That's a good idea. It's not a perfect solution, but it might be the best thing that will work. – Peeja May 07 '09 at 00:10
  • 2
    `mousemove` is triggered even if you move your mouse on a blurred window, use `mousedown` instead. Try it here: http://www.quirksmode.org/dom/events/tests/mousemove.html – Ali Shakiba Jun 29 '11 at 12:25
0

The onfocus event should be triggered when the page is initially opened (in the foreground). At least it is in FF3/IE8.

So you could have sounds on initially, turn them off when onfocus is triggered, and turn them back on when onblur is triggered.

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
  • Even when someone does an open-to-new-tab with the switch-to-new-tab preference off? (Not saying it doesn't, asking because I want to know. :) – chaos May 06 '09 at 21:08
  • For me, when I opened in a new tab (in the background), onfocus was not triggered. Once I switched to the tab, it triggered. So if you start with sounds on, that should work, right? If the tab is opened in the foreground, sounds will be on, but the onfocus event will turn them off immediately. If the tab is opened in the background, sound stay on until you switch to that tab. – Patrick McElhaney May 06 '09 at 21:13
  • I see that in FF3, but not in Safari. Safari doesn't give me an onfocus until the window actually gains focus after being in the background. – Peeja May 07 '09 at 00:09