Log out with Javascript Event when closing the window

2

Situation

I have a system in which I need the user to be logged off when I close the browser.

Code

In this way I managed to get the browser closing, or rather to 'exit' the page for a new one (or not in case of browser closing). But it's blocking my confirmation.

var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compatable

    myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
      var confirm = window.confirm('Deseja fazer logout?');
      if(confirm){
        logout();
      }
    });

    function logout (){
      console.log('Logout');
    }

Error

  

Blocked confirm ('Do you want to logout?') during beforeunload

Anyone else have a way to do this?

    
asked by anonymous 24.06.2015 / 18:53

1 answer

2

The window.confirm will not work because the onbeforeunload structure already has a return for the confirmation screen, I believe the correct one would be this:

window.onbeforeunload = function() {
    return "Gostaria mesmo de sair?";
};

But note that it will not be possible to fire events if you click "Ok" because the window will already have closed, the moment the window or tab closes its instance is destroyed and for this reason it is no longer possible to send events because it no longer "exists".

So if you want to create an event that will disable the user of your system (assuming you are using php , .net , jsf , etc) you will need to create a "timer" to expire the user, a good time would be "2 mintuos", please note that the Google Analytics uses a technique similar to the one I mentioned.

In the author's case, you are using Ruby On Rails , you can try this (as is the response from SOen ):

Authlogic can do this by default. I suggest you migrate your authentication system (it may take some time depending on how much your system is customized).

This example also exists link

    
24.06.2015 / 19:48