有没有可靠的方法来确定浏览器选项卡或窗口是否不活动或不在焦点? [英] Is there a reliable way to determine if a browser tab or window is inactive or not in focus?

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

问题描述

我有一个javascript计时器,它不断地(每10秒一次)发出XMLHTTP请求。

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

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.

下面的简单代码提供了我正在寻找的功能:

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>

有没有人有一种技术来确定浏览器窗口或标签何时失去/ c> Safari v3.0.4

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>(WebKit 530+),该bug已经解决了似乎。我已在 Google Chrome v3.0.195.27 中检查过它,它有相同的Safari错误,虽然它有一个较新版本的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天全站免登陆