确定网页是否有前景窗口焦点/活动标签? [英] Determine whether webpage has foreground window focus/is active tab?

查看:169
本文介绍了确定网页是否有前景窗口焦点/活动标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我能检测到页面不再集中,我希望能够静音我的应用程序的活动。例如,如果页面位于后台选项卡或其他应用程序有焦点,我想禁用一个不断的轮询脚本或切换模式通知到新的HTML5通知API。



有什么办法可以得到这个与JS,如果是这样,支持哪些浏览器?



PS - 我见过

您可以侦听 blur 事件在你的窗口上,那么当用户回来时,你可以使用焦点事件:



下面是jQuery中的一个例子:

  $(window).blur(disableStuff)对焦(enableStuff); 

或者在纯JavaScript中:

  window.onblur = disableStuff; 
window.onfocus = enableStuff;


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 blur event on your window, then for when the user comes back, you can use the focus event:

Here's an example in jQuery:

$(window).blur(disableStuff).focus(enableStuff);

Or in pure JavaScript:

window.onblur = disableStuff;
window.onfocus = enableStuff;

这篇关于确定网页是否有前景窗口焦点/活动标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆