I implemented a counter that shows the number of new chat messages in the page title, i.e. in the tab of the browser is is displaying: "(2) Chat - My Forum"
// add a message to the list
function qa_chat_add_message( post )
{
var $ex_post = $( '#qa-chat-id-'+post.postid );
if ( $ex_post.length == 0 )
{
var $msg = $( Tmpl.message( post ) );
$('.qa-chat-item-when', $msg).timeago();
$('#qa-chat-list').prepend($msg);
$msg.slideDown('fast');
// write count of new chat messages in page title, do not regard if post is not by user himself
if(!userPostsMessage) {
var rowCount = $('ul#qa-chat-list li:visible').length - userPostsCount;
if(startEventsVisible) { rowCount -= startEventsCount - userPostsCount; }
$('title').text('('+rowCount+') '+ pagetitle);
}
}
/*if ( $('.qa-chat-item').length > 80 )
$('.qa-chat-item:nth-child(n+81)').remove();
*/
}
var startEventsVisible = true;
var startEventsCount = 0;
var firstLoad = true;
var pagetitle = 'Mathe-Chat - Gute-Mathe-Fragen';
var userPostsMessage = false;
var userPostsCount = 0;
// fetch all new messages
function qa_chat_fetch_messages()
{
$.ajax({
type: 'post',
data: { ajax_get_messages: lastid },
success: function(response) {
var lines = response.split("\n");
if ( lines[0] != 'QA_AJAX_RESPONSE' || lines[1] == 0 )
return false;
cUser.id = lines[1];
var posts = $.parseJSON( lines[2] ).reverse();
for ( var i in posts ) {
qa_chat_add_message( posts[i] );
lastid = posts[i].postid;
}
// update active users
if ( lines[3] ) {
var users = $.parseJSON( lines[3] );
qa_chat_update_users( users );
}
if(firstLoad) {
startEventsCount = $('ul#qa-chat-list li:visible').length;
$('title').text('(0) '+ pagetitle);
firstLoad = false;
}
}
});
// adding a message to the chat
$('#qa-chat-form').submit( function() {
var message = $('#message').val();
if ( message.length == 0 )
return false;
$('#qa-chat-form input').attr({ disabled: 'disabled' });
$.ajax({
type: 'post',
data: { ajax_add_message: message, ajax_add_lastid: '0' },
success: function(response) {
$('#qa-chat-form input').removeAttr('disabled');
$('#message').val('').focus();
var lines = response.split("\n");
if ( lines[0] != 'QA_AJAX_RESPONSE' ) {
alert("There was a server error, please try again in a few minutes");
return false;
}
if ( lines[1] == 0 ) {
alert("Error: "+lines[2]);
return false;
}
userPostsMessage = true;
userPostsCount++;
var post = $.parseJSON( lines[2] );
qa_chat_add_message( post );
userPostsMessage = false;
cUser.idle = false;
}
});
return false;
} );