admin管理员组

文章数量:1427321

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?

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?

Share Improve this question asked May 6, 2009 at 20:53 PeejaPeeja 14.3k11 gold badges62 silver badges78 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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)?

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.

本文标签: javascriptHow can I detect whether a browser window is focused or notStack Overflow