简体   繁体   English

如何使用Javascript检测Chrome中何时关注标签?

[英]How to detect when a tab is focused or not in Chrome with Javascript?

I need to know if the user is currently viewing a tab or not in Google Chrome. 我需要知道用户当前是否正在Google Chrome中查看标签。 I tried to use the events blur and focus binded to the window, but only the blur seems to be working correctly. 我试图将事件模糊和焦点绑定到窗口,但只有模糊似乎正常工作。

window.addEventListener('focus', function() {
  document.title = 'focused';
});

window.addEventListener('blur', function() {
  document.title = 'not focused';
});

The focus event works weird, only sometimes. 焦点事件很奇怪,只是有时候。 If I switch to another tab and back, focus event won't activate. 如果我切换到另一个选项卡并返回,焦点事件将不会激活。 But if I click on the address bar and then back on the page, it will. 但是,如果我点击地址栏然后返回页面,它就会。 Or if I switch to another program and then back to Chrome it will activate if the tab is currently focused. 或者,如果我切换到另一个程序然后返回到Chrome,则会激活该选项卡当前是否已关注。

2015 update: The new HTML5 way with visibility API (taken from Blowsie's comment): 2015年更新:具有可见性API的新HTML5方式(取自Blowsie的评论):

document.addEventListener('visibilitychange', function(){
    document.title = document.hidden; // change tab text for demo
})

The code the original poster gives (in the question) now works, as of 2011: 原始海报给出的代码(在问题中)现在有效,截至2011年:

window.addEventListener('focus', function() {
    document.title = 'focused';
});

window.addEventListener('blur', function() {
    document.title = 'not focused';
});

edit : As of a few months later in Chrome 14, this will still work, but the user must have interacted with the page by clicking anywhere in the window at least once. 编辑 :几个月后,在Chrome 14中,这仍然有效,但用户必须通过点击窗口中的任意位置至少一次与页面进行交互。 Merely scrolling and such is insufficient to make this work. 仅仅滚动这样做是不够的。 Doing window.focus() does not make this work automatically either. 执行window.focus()也不会自动执行此操作。 If anyone knows of a workaround, please mention. 如果有人知道解决方法,请提及。

The selected answer for the question Is there a way to detect if a browser window is not currently active? 问题的选定答案是否有办法检测浏览器窗口当前是否处于活动状态? should work. 应该管用。 It utilizes the Page Visibility API drafted by the W3C on 2011-06-02. 它利用W3C于2011-06-02起草的Page Visibility API

It might work after all, i got curious and wrote this code: 毕竟它可能会起作用,我很好奇并编写了这段代码:

...
setInterval ( updateSize, 500 );
function updateSize(){
  if(window.outerHeight == window.innerHeight){
    document.title = 'not focused';             
  } else {
    document.title = 'focused';
  }

  document.getElementById("arthur").innerHTML = window.outerHeight + " - " + window.innerHeight;
}
...
<div id="arthur">
  dent
</div>

This code does precisly what you want, but on an ugly way. 这段代码正是你想要的,但却是一种丑陋的方式。 The thing is, Chrome seems to ignore the title change from time to time (when switching to the tab and holding the mouse down for 1 sec seems to always create this effect). 问题是,Chrome似乎不时忽略标题更改(当切换到选项卡并按住鼠标1秒似乎总是产生这种效果)。

You will get different values on your screen, yet your title won't change. 您将在屏幕上获得不同的值,但您的标题不会更改。

conclusion: Whatever you are doing, don't trust the result when testing it! 结论:无论你做什么,在测试时都不要相信结果!

For anyone who wants to swap page titles on blur and then go back to the original page title on focus: 对于想要在模糊时交换页面标题然后返回焦点上的原始页面标题的任何人:

// Swapping page titles on blur
var originalPageTitle = document.title;

window.addEventListener('blur', function(){
    document.title = 'Don\'t forget to read this...';
});

window.addEventListener('focus', function(){
    document.title = originalPageTitle;
});

This could work with JQuery 这可以与JQuery一起使用

$(function() {
    $(window).focus(function() {
        console.log('Focus');
    });

    $(window).blur(function() {
        console.log('Blur');
    });
});

我发现添加onblur =和onfocus =事件以内联绕过了问题:

In chrome you can run a background script with a timeout of less than 1 second, and when the tab does not have focus chrome will only run it every second. 在chrome中,您可以运行时间小于1秒的后台脚本,当选项卡没有焦点时,chrome只会每秒运行一次。 Example; 例;

This doesn't work in Firefox or Opera. 这在Firefox或Opera中不起作用。 Don't know about other browsers, but I doubt it works there too. 不知道其他浏览器,但我怀疑它在那里也有效。

var currentDate = new Date();
var a = currentDate.getTime();

function test() {
    var currentDate = new Date();
    var b = currentDate.getTime();
var c = b - a;
    if (c > 900) {
        //Tab does not have focus.
    } else {
        //It does
    }
    a = b;
    setTimeout("test()",800);
}



setTimeout("test()",1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM