In SO, when your question got answer. or you got new badge, event is triggered. Or when you got new PM in forum, it also lets you know by alerting message.
You see message that something happened with your account when you enter site for first time after this event.
How is this implemented? How do scripts know, that they have something new to show you?
The programming technique you are looking for is called Comet. The link to wikipedia describes some implementations of that, but the easiest way is to make an XMLHttpRequest with a long timeout and only return data on change.