Page title Notification in multiple browsers
I have a chat script on a webpage. Now, when someone adds a message I want to notify other people who have the page/tab open but not active, to receive a notification of a new message.
For instance, I would like to change the page title from 'Chat' to开发者_开发知识库 'New message - Chat'. I think this is what 'Facebook Chat' does too.But when I use: document.title = "New Message"; It only changes in the browser of the user that submitted the message. Not on the other browsers.
How can I achieve this?
You can give your title an id:
document.getElementById("title").innerHTML ="new Title";
or simply
document.getElementsByTagName('title')[0].innerHTML = "New Title";
either way it will work
UPDATE:
after rereading i made a mistake i see.
when your script check for a new message, if there is a new message you should change the title also, or perform additional test to see if there the user is active or not before changing the title
To do what Facebook Chat does, which is notify each user of updates in close to real time, you need to use "push" technology or something that emulates it. Possible techniques are polling (just having each client regularly hit the server to check for updates), Comet (long polling or streaming connections to the server), or newer and more authentically "push" (but less supported) methods like server-sent events or the WebSocket API.
Polling is not the most efficient technique but will be the simplest to implement. You could start by writing a server-side handler that accepts a timestamp, and returns a count of updates since that timestamp. Client Javascript calls that handler at an interval, passing the timestamp of the last check, and handling the response (updating the title bar if there were recent updates found).
Listen document.title = "New Message";
is an absolutely perfect code but you've put this in such an area that when a person types a message, it is executed on his page which is wrong. You should add this piece of code when the user waiting for a reply (the other user, you're chatting with) when the AJAX (or any other method you use to check) receives a response from the person who has typed the message.
Basically what I'm saying is that this code piece has been placed in the wrong place. You must place this code piece when a person received the chat message and not when it is dispatched to another person.
And then later on body, you can make event when onmousemove
change the title back to the original one! So this is how Facebook Chat works.
Your mistake is the place, you've place your code piece, nothing else!
Hope this helps, Cheers!
精彩评论