最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing the window title when focussing the window doesn't work in Chrome - Stack Overflow

programmeradmin0浏览0评论

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 resetting, 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.

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 resetting, 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.

Share Improve this question asked Jun 1, 2010 at 18:47 balphabalpha 50.9k18 gold badges113 silver badges132 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 15

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.

发布评论

评论列表(0)

  1. 暂无评论