How to do X when user closes the page

This is also one of the common questions and for some reason many people don't know how to achieve this. For instance, you would like to write something to the database (if you are doing statistics for example) when the user leaves the page. In a case like this you would use the onBeforeUnLoad and a AJAX call to the server that will do the rest. Keep in mind that this should not be used to do some heavy client side processing, because if the user decided to leave our page you must let him, you don't have the right to stall him and bug him with annoying pop-ups asking for confirmation (but there are exceptions, like this editor that I am using right now, if it didn't have a pop-up that asked me if I really wanted to leave the page with unsaved changes I would loose a lot of my posts).

So if you would like to do things like that just use the onBeforeUnLoad
Avtor: Anonymous, objavljeno na portalu SloDug.si (Arhiv)

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.