This sounded like something almost impossible to do when it was presented to me. I know you can display a dialog box to confirm when leaving a web page. But is it possible to display a dialog box when leaving a site?
I haven't been able to find/create anything that can read the address bar and know that you're leaving the site.
First off define which events can actually take your user away from your site?
So. what can you do about all these?
onclick
eventonsubmit
event of the form posting back outside of your site.onbeforeunload
event as much as you want, but you won't have much success of knowing what's going on. And there are certain limitations related to onbeforeunload
as well, so your hands will be tied most of the time.Why would you want to control this event anyway except for bothering your users not to leave you. Begging doesn't give much justice in the web world anyway. And when some site would bother me with messages or even worse prevent me from leaving I wouldn't want to get back anymore. It smells of bad bad bad usability and gives a hint of adware site.
Rather try to keep your users interested by providing them with valuable content.