【问题标题】:Determine if browser tab is active? - IE?确定浏览器选项卡是否处于活动状态? - IE?
【发布时间】:2011-10-22 04:42:07
【问题描述】:

我看过这个:

How to tell if browser/tab is active

和:

Is there a reliable way to determine if a browser tab or window is inactive or not in focus?

第一个链接为现代浏览器提供了一个解决方案,但在 IE7/8 中不起作用。这两个问题都相当老了。确定访问者是否正在查看他们打开的标签的问题是否有解决方案?

我尝试过的几乎所有东西都可以在 Chrome 中正常运行。但是 IE7 就失败了。

我只想设置一个全局变量来说明是否正在查看页面。

即

var isActive = true;

$(window).focus(function() {
    isActive = true;
});

$(window).blur(function() {
    isActive = false;
});

// test
setInterval(function () {
  console.log(window.isActive ? 'active' : 'inactive'); 
}, 1000);

【问题讨论】:

    标签: javascript tabs focus


    【解决方案1】:

    在谷歌上搜索了很多...然后更多...然后更多...4 小时左右后我终于找到了隐藏在互联网深处的这个链接。 cmets 表明存在一个小错误,但它可以满足我的需要。

    http://www.thefutureoftheweb.com/blog/detect-browser-window-focus

    var isActive = true;
    function onBlur() {
        isActive = false;
    };
    function onFocus(){
        isActive = true;
    };
    
    if (/*@cc_on!@*/false) { // check for Internet Explorer
        document.onfocusin = onFocus;
        document.onfocusout = onBlur;
    } else {
        window.onfocus = onFocus;
        window.onblur = onBlur;
    }
    

    【讨论】:

      【解决方案2】:

      略有不同,但this was marked as the answer 暗示它有效,这是需要的延迟。不是很优雅,但如果它有效的话。

      【讨论】:

      • 也许我的问题不清楚。我不想打开另一个窗口。我只想在浏览器未查看选项卡时停止事件。然后在他们返回选项卡时恢复它们
      猜你喜欢
      • 2010-12-18
      • 2020-11-23
      • 2013-10-31
      • 2010-11-15
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多