Hi,
I need to take some action when the user leaves my website.
By leave I mean one of the following two cases:
- Closes the browser
- Goes to some other site (different domain)
Is there a way to detect this?
I know that the event onbeforeunload and onunload gets fired when the user leaves the page, but these also get fired when the user leaves page A in my site and just goes to page B in my site.
Can anyone think of a way to solve this?
I don’t want to alter every link within my site to fire the onclick event and in such event to “remember” in a JavaScript variable this is a navigation within my site.
I am looking for some clean solution if one exists.
For example, is there a way to get the information in the DOM where the user is going to when the onbeforeunload or onunload are fired?
thanks
Jason
Yes, you can use the onbeforeunload
event (search for it). Beware that you should only use it in the most exceptional circumstances, and this is only used normally by applications such as Gmail that use lots of Ajax and navigation without actually leaving the page. It is acceptable for something like Gmail.
The other type of website that uses this is annoying websites that want to try to keep their visitors there as long as possible. This is likely to irritate the visitor and alienate them from the site.
I don’t want to alter every link within my site to fire the onclick event and in such event
This sounds like you’re using inline event handlers (onclick=“…”) in your HTML. This makes maintenance time-consuming and your pages liable to break more. You should use unobtrusive javascript (search for it).
You can’t detect where the user is going to, unless you listen to onclick events on every link on the page. It still doesn’t allow you to detect if the user typed “sitepoint.com” into the address bar, but they will still get an annoying “are you sure you want to leave my wbesite?” message.
If you want to exclude links that point to pages on your domain, that would be simple to do by checking whether the links on your page are relative links or if they point to your domain (simple string manipulation).
I strongly recommend that you don’t implement this “feature” unless you feel it is useful to the user, e.g. if they have been filling out a lengthy form.
Please do a search, this question is asked often.
No. For security reasons JavaScript is not told whether the page is unloading to close the browser, load a different page, or just reload the same page.
I would advise against using unload events. But I suppose you have your reasons. The simplest and most know is “onunload” in the body tag.
<body onUnLoad="do something here">
e.g.
<html>
<body onUnLoad="alert('leaving so soon?');">
Body text
</body>
</html>
Remember users will require JavaScript for it to work. But I believe that remains true for almost all approaches to your current question.
One consideration when using onunload is that the unload of the page has already started by the time it is triggered. That means that you cannot provide processing to abort the unload and if you have more than one or two statements in the code then it is possible that the code will be unloaded before it has time to run.
Using onbeforeunload avoids those problems but it is a proprietary event that is not a part of the standards and so only some browsers support it.
In either case the code will only run if JavaScript is enabled and then it will run regardless of the reason why the page is unloading. Another thing to consider is that if the code you insert there requires user interaction then you run the risk of annoying that person so that they will not return to your site.