2012-01-10 18 views
0

我想要在標題部分的通知數量(像Facebook一樣),但是我還沒有能夠想出一個很好的方法來做到這一點。幾年前我做過這個,但是我做的方式並不是那麼好,它會混亂並且在某個時候刪除錯誤的部分。所以我試圖想出最簡單的方法來做到這一點。更新部分標題標籤,但不是全部,或者至少是一個很好的解決方法?

並且標題的值是動態的,所以我不能僅僅通過一種方式手動完成它。

所以像:

(8)的MySite - 主頁

那麼當它改變它只想去,如: 的MySite - 主頁或mysite的 - UserID102

我曾經做過是這樣的:

var got_title = $('title').html(); 
    var find_notifications = got_title.match(/\([0-9]\)\s/); ///look for (1-9) 

    if(find_notifications) 
    { 
     var default_title = got_title.split(' ')[1]; 
    } 
    if(!find_notifications) 
    { 
    var default_title = got_title; 
    } 
    $('title').html(update_notification+default_title); 

但是這是非常不可靠的,有時它工作得很好,但有時它會不斷增加的通知,如:

的MySite - 首頁 - >(5)的MySite - 首頁 - >(5)(5)(5)(5)(5)(5)的MySite - 首頁

回答

1

我不知道的update_notification內容,但也許這可以幫助你:

var got_title = $('title').html(); 
var default_title = got_title.match(/(\(\d+\)\s)?(.*)/)[2]; 
$('title').html(update_notification+default_title); 

也看到這個example

+0

如果我沒有記錯,「update_notification」是(1)或任何通知數可能爲 – 2012-01-10 07:57:47

+0

update_notification具有新的通知數? – scessor 2012-01-10 08:03:37

+0

我已經添加了一個[示例](http://jsfiddle.net/U2sKr/1/)。 – scessor 2012-01-10 08:06:23

相關問題