如何检测浏览器窗口是否关注? [英] How can I detect whether a browser window is focused or not?
问题描述
仅当窗口在后台时,我的页面才会发出通知声音。我可以跟踪 window.onfocus
和 window.onblur
事件,以便在焦点状态发生变化时注意。但是,我不知道窗口是否会加载焦点,例如,因为它可能会加载到背景选项卡中。
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.
我如何决定是否播放在我得到 onfocus
/ onblur
事件之前听到与否?
How do I decide whether to play sounds or not before I get an onfocus
/onblur
event?
推荐答案
难道你不能假设它被加载模糊,然后在收到任何类型的事件(keydown / mousemove)后立即将状态更改为焦点吗?
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)?
这篇关于如何检测浏览器窗口是否关注?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!