确定网页是否有前景窗口焦点/活动标签? [英] Determine whether webpage has foreground window focus/is active tab?
问题描述
如果我能检测到页面不再集中,我希望能够静音我的应用程序的活动。例如,如果页面位于后台选项卡或其他应用程序有焦点,我想禁用一个不断的轮询脚本或切换模式通知到新的HTML5通知API。
有什么办法可以得到这个与JS,如果是这样,支持哪些浏览器?
PS - 我见过 您可以侦听 下面是jQuery中的一个例子: 或者在纯JavaScript中: I'd like to be able to mute activity of my app if I can detect that the page is no longer focused. For instance, if the page is in a background tab or another app has focus, I'd like to disable a constantly polling script or switch modal notifications to the new HTML5 notifications API. Is there any way to get this with JS, and if so, which browsers are supported? PS - I've seen this, but don't know if it would work for what I'm looking to do. Anybody have any insight? You can listen for the Here's an example in jQuery: Or in pure JavaScript:
这篇关于确定网页是否有前景窗口焦点/活动标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! blur
事件在你的窗口
上,那么当用户回来时,你可以使用焦点
事件:
$(window).blur(disableStuff)对焦(enableStuff);
window.onblur = disableStuff;
window.onfocus = enableStuff;
blur
event on your window
, then for when the user comes back, you can use the focus
event:$(window).blur(disableStuff).focus(enableStuff);
window.onblur = disableStuff;
window.onfocus = enableStuff;