问题
(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