I want to determine when user is leaving the site using javascript. By site I means domain here.
The body unload occur before leaving any page. So it won’t do the job. But is there any way to capture where the user is headed? So when the address is on another domain something is alerted by this event.
Another way, attach an onclick or onsubmit to any inner form and inner link. On this events we set a variable so on unload understand that we are going to an inner page. Maybe you suggest a script that do that automatically but without any javascript framework.
Dears, I don’t want to use this as a “please, stay on my site”. I want to ask those user that go to order page but anyhow can’t submit and finalize the order, and leaving our site to contact us on the phone. So please don’t judge it.
If a user types in the URL into his browser, you have no way of knowing what was requested. If he clicks an external link, you again have no way of detecting AFAIK.
However, you can create a redirect page on your server and rewrite all links on the page to point to that page, for instance
www.ExternalSite.comis changed tohttp://www.example.com/redirect.php?u=www.ExternalSite.com. This is how Facebook used to do it.