To check if the page is focused you can use the document.hasFocus
function. It returns true
if the document is focused and false
otherwise.
It doesn't appear to be supported in Opera though, so if supporting Opera is important to you you can watch the focus
and blur
events of window
to determine if the window is in focus or not (here is an example.)
On the topic of "how to alert the user", that's really up to you to decide. You could do it the normal way, prepending a "(x)" to your title, where x is the number of new messages. You could also alert the user with a noise using Flash or even better, the JavaScript Audio API:
var sound = new Audio('sound-file.wav');
sound.play();
Be sure to keep audio feedback strictly voluntary though.
If you want to be extra fancy you could have a look at Chrome desktop notifications. Any user running Chrome could get a desktop notification (The same kind you get when you receive a Skype or MSN message) whenever a new message arrives. Here is an example of usage.