Here is how to make the browser title blink when a new chat message arrives:
We are going to edit right.jsp. Here is the part that we are going to edit:
- Code: Select all
<c:if test="${model.showChat}">
<script type="text/javascript">
var revision = 0;
startGetMessagesTimer();
function startGetMessagesTimer() {
chatService.getMessages(revision, getMessagesCallback);
setTimeout("startGetMessagesTimer()", 10000);
}
function addMessage() {
chatService.addMessage($("message").value);
dwr.util.setValue("message", null);
setTimeout("startGetMessagesTimer()", 500);
}
function getMessagesCallback(messages) {
if (messages == null) {
return;
}
revision = messages.revision;
// Delete all the rows except for the "pattern" row
dwr.util.removeAllRows("chatlog", { filter:function(div) {
return (div.id != "pattern");
}});
We are going to add a function, and change an existing function.
First, add this function:
- Code: Select all
function blinkTitle() {
if (blink && msgLength > 0) {
top.document.title = "New Message!";
setTimeout("top.document.title = 'Subsonic'",1500);
setTimeout("blinkTitle()",3000);
}
}
Next, we need to edit the function getMessagesCallback(). Add these variables right after "var revision = 0;" like this:
- Code: Select all
<c:if test="${model.showChat}">
<script type="text/javascript">
var revision = 0;
var revision2 = 0;
var blink = 0;
var msgLength = 0;
startGetMessagesTimer();
Now add "msgLength = messages.messages.length;" one line after "function getMessagesCallback(messages) {".
Now, we are going to add this code:
- Code: Select all
if (revision2 != revision) {
revision2 = revision;
if (messages.messages.length > 0 && !blink) {
blink = 1;
blinkTitle();
} else {
blink = 0;
}
var frames = top.window.frames;
for (var i = 0; i < 5; i++) {
frames[i].addEventListener('focus', function() {
top.right.blink = 0;
top.document.title = 'Subsonic';
}); }
}
right after this code:
- Code: Select all
function getMessagesCallback(messages) {
msgLength = messages.messages.length;
if (messages == null) {
return;
}
Also, we have to add "revision2=revision" to the textbox so that it looks like this:
- Code: Select all
<input class="text" id="message" value="<fmt:message key="main.message"/>" style="width:90%" onclick="select()" onkeypress="dwr.util.onReturn(event, addMessage); revision2=revision;"/>
Some things may be a little weird because we are working with focusing 5 frames. Sometimes you just have to click in any frame (besides for the right frame sometimes

) to get the blinking to stop.
Hope this helps!
