You can use the onbeforeunload event (where supported). However, a word of warning: only use it where the user will thank you for it. In other words, use responsibly. There are plenty of cases of misuse which only annoy the user pretty much guaranteeing that you've lost another visitor.
Comment