有没有一种可靠的方法来确定一个浏览器标签或窗口不活动或在重点是什么? [英] Is there a reliable way to determine if a browser tab or window is inactive or not in focus?

查看:192
本文介绍了有没有一种可靠的方法来确定一个浏览器标签或窗口不活动或在重点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这是一个恒定的基础上(每10秒一次)上做出XMLHTTP请求的Jav​​aScript计时器。我很想能够暂停计时器时,窗口或标签失去焦点。

I've got a javascript timer that is making XMLHTTP requests on a constant basis (once every 10 seconds). I'd love to be able to pause the timer when the window or tab loses focus.

我在窗口<充分认识到的onfocus 的onblur 事件/ code>对象,但他们不可靠的在所有浏览器触发。例如,在Safari ,标签不触发事件的。

I'm fully aware of the onFocus and onBlur events on the window object, but they don't fire reliably across all browsers. For instance, in Safari, tabs don't trigger the events.

下面简单code提炼我在寻找的功能:

Simple code below distills the functionality I'm looking for:

<html>
  <head>
    <title>Testing</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>
  </head>
  <body>
    <div id="console"></div>
    <script type="text/javascript">
      window.onfocus = function(event) {
        $('console').insert('Window gained focus<br />');
      }

      window.onblur = function(event) {
        $('console').insert('Window lost focus<br />');
      }
    </script>
  </body>
</html>

有没有人在那里有一个确定技术时,一个浏览器窗口或选项卡丢失/收益焦点在所有流行的浏览器可以跨越?

Does anyone out there have a technique for determining when a browser window or tab loses/gains focus that works across across all popular browsers?

推荐答案

以上code在 Safari浏览器v3.0.4做工精细(WebKit的530+)这个bug已经解决它似乎。我在谷歌浏览器v3.0.195.27 检查,它具有相同的Safari浏览器的bug,虽然它具有的WebKit的更新版本。

The above code is working fine in Safari v3.0.4 (WebKit 530+), the bug has been resolved it seems. I've checked it in Google Chrome v3.0.195.27 and it has the same Safari bug, although it has a newer version of WebKit.

这篇关于有没有一种可靠的方法来确定一个浏览器标签或窗口不活动或在重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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