0

I know that (using Jquery) you cant detect when your page is about to unload for any reason using the beforeUnload event

eg

$(window).on('beforeUnload' , function ( e ) {
            return "please dont leave";
});

Is it possible to detect if this was caused by something outside of your page such as closing the tab/window or using the back/forward buttons.

If use of such things cannot be detected normally is it possible to determine with certainty that beforeUnload was NOT triggered by any event on any DOM element?

megaman
  • 1,035
  • 1
  • 14
  • 21

1 Answers1

0

Isn't easy to detect external actions like browser actions, you can do the inverse.

example - track if is a link

 var el = null;
    $('a,.submit-button,.other-exit-buttons').click(function(e){
    el=e;
    });

$(window).on('beforeUnload' , function ( e ) {
            console.log(el);
            // do other stuff like log it
            return "please dont leave";
});

So you can check if users exit throw anchor tag or you can use this method to track all exit from 'submit' buttons for example.

Map your main exit events, not all the site, if a user exit and your var is still 'null' you know that user exit in another way.

Alfredo Lanzetta
  • 314
  • 4
  • 18