Detect Leaving Visitors with jQuery
Users leaving your website is never a good thing, but there are some scenarios where you may need to catch them before they head elsewhere on the web as a result of them clicking a link on your site.
Let’s imagine that you want to bring to the users attention the fact they are leaving your website and that you are not responsible for the content of the site they are heading to. This may be particularly useful for websites belonging to legal or pharmaceutical companies, or for sites that display user-submitted URL’s such as directories.
By using jQuery we can quickly and easily detect this scenario. Simply copy and paste the code below anywhere on a page for it to work:
Please note, this script does not detect if a user closes the window or leaves your site by another means, such as by going direct to another website by typing it’s URL in the address bar.