Changing the window title when focussing the window doesn't work in Chrome
I'm (ab)using the document title as some kind of a notification area. If the tab/window in question doesn't have focus at the time I want to notify the user of something, I change the window title to include some notification.
When the window/tab comes back into focus, I want to remove the notification, i.e. reset the title to standard. This rese开发者_如何转开发tting, however, sometimes doesn't work in Chrome.
When I come from a different window, the title gets updated correctly, but when I come from a different tab within the same window, it doesn't.
I have found a workaround for this (I'll post it as an answer), but please share any other ideas you might have to fix this.
This appears to be a bug in Chrome regarding the actual redrawing of the tab title, since the document title itself (i.e. within the DOM) is in fact updated correctly.
I suppose that changing the tab (which causes the tab to move to the foreground, hence to be redrawn) and changing the title within the resulting focus event (which also requires a redraw) cause some kind of race condition.
That's why I tried this workaround, which does in fact work:
window.setTimeout(function () { $(document).attr("title", newtitle); }, 200);
– just wait for a short time before updating the title, so the two redraw events don't conflict with each other. That's not pretty, obviously, but a 0.2 second delay shouldn't usually be a problem.
although tiftik said this bug is fixed, it still happend at my Chrome (Version 21).
The solution balpha offered works for me too - but be aware not to set the document title to the new title on Focus and than wait 200ms to set it again - This won't work! (probably because Chrome thinks it is the same title, so it won't update)
Just wait 200ms and set it - works fine!
For future reference: this bug is currently fixed.
精彩评论