简体   繁体   中英

How can I detect whether a browser window is focused or not?

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?

难道你不能假设它被加载模糊,然后在收到任何类型的事件(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.

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