window focus not working in chrome when switch tabs

拈花ヽ惹草 提交于 2019-12-25 10:26:32

问题


(function($){

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

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

})(jQuery);

What it's supposed to do: if a tab is focused, the title should be 'focused'; if it's not, then title should be 'not focused'.

What it's doing in reality: after leaving a tab unfocused, its title will remain forever 'not focused'.

This is working fine in Firefox, but not in Chrome, anyone got suggestions? Thanks!


回答1:


Check out this answer, you could easily modify that code to complete the task you've outlined here.




回答2:


This is in fact a reported bug of Chrome and it is happening in version 29, when you change between tabs. It seems to be a race condition that will not update the title of the tab, despite the fact that document.title is correctly filled.

A possible workaround for the code above is to create a timer to change the title again. The following code works, but you can also just set the text inside the timer function.

(function($){

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

$(window).blur(function(){
    document.title = 'not focused';

    window.setTimeout(function () {
        var temp = document.title;
        document.title = temp + "_";
        document.title = temp;
    }, 200);
});

})(jQuery);


来源:https://stackoverflow.com/questions/17757157/window-focus-not-working-in-chrome-when-switch-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!