2013-02-21 2 views
-1

При получении уведомления в фейсбуке правоустанавливающий документ будет обновляться автоматическиКак обновить название документа как facebook и удалить его правильно?

(1) Facebook

Как сделать это с помощью JavaScript?

UPDATE: Я хочу знать, как вставить (N) и удалить его, когда пользователи нажимают на указанный div.

+0

Обновлен мой вопрос. – Chris

ответ

5

Существует простой способ обновить document title:

document.title = 'My new title'; 

Вот фрагмент кода, который будет обновлять название, когда новое уведомление доступно и обновить его еще раз (удалить ряд новых уведомлений), когда пользователь нажимает на div #foo.

var title = document.title; 
var notifications = 0; 
jQuery.post(url, data, function(response) { 


    // this regex will test if the document title already has a notification count in it, e.g. (1) My Document 
    if (/\([\d]+\)/.test(title)) { 
     // we will split the title after the first bracket 
     title = title.split(') '); 
     // get the first part of the splitted string and save it - this will be the count of the unseen notifications in our document string 
     notifications = title[0].substring(1); 

     // only proceed when the notification count is difference to our ajax request 
     if (notifications == response) 
      return; 
     // else update the title with the new notification count 
     else 
      document.title = '(' + sp_response + ') ' + title[1]; 
    } 
    // when the current document title does not contain any notification count, just update it 
    else { 
     document.title = '(' + sp_response + ') ' + title; 
    } 

}); 

jQuery('#foo').click(function(event){ 
    var title = document.title; 
    if (/\([\d]+\)/.test(title)) { 
     title = title.split(') '); 
     document.title = title[1]; 
    } 
}); 
Смежные вопросы