Using websocket to detect page exit

This might be a silly question, but I read that using the onbeforeunload event (to detect when a page is closed or moved away from it) is unreliable. I am trying to record this event for a pageview tracking application. Something else I was thinking about was opening a websocket connection and then writing when that connection is closed. Is this something that will work, or is it too complicated and will add too much extra overhead to the page? The app uses the javascript tracking code inserted into the tracking page and registers itself with the Rails backend.

+3


source to share


1 answer


This would indeed create too much overhead, but it might be helpful to try and do it with jQuery. See if the following works for you:

$("html").mouseleave(function(){
    //Do something
});

      



While this may only work when the page is moved but not closed (although I'm not 100% sure on this), this is a start.

+1


source







All Articles