Logout with Javascript Event when closing the window

Asked

Viewed 1,585 times

2

Situation

I have a system in which I need the user to be dropped when closing the browser.

Code

in this way I was able to catch the closing of the browser, or better to say the 'exit' of the page for a new ( or not in case of browser closure ). But it is blocking me in the 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 log out?') During beforeunload

Someone would have another way to effect this functionality?

  • What do you mean Porém está me barrando na confirmação?

  • Would the method I call window.confirm() he blocks by being in beforeunload. Confirmation would be the user’s reply whether or not to logout, I want to treat and below make the decision to go to my logoff or continue on the page.

  • What do you use to keep the user connected to the system? sessions ?

  • is by Cookies I use TOKEN. But my problem itself is not to dislodge but the function inside. = D

  • Good evening, I wonder if the answer helped you, if not please comment on what you think is missing.

1 answer

2

The window.confirm will not work because the structure of onbeforeunload already owns a return to the confirmation screen, I believe that the correct would be this:

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

However note that it will not be possible to trigger events if you click on "Ok", because the window will already be closed, by the time 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 depress the user from your system (assuming you are using php, .net, jsf, etc) it will be necessary to create a "timer" to expire the user, a good time would be "2 mintuos", note that the Google Analytics uses a technique similar to the one I mentioned.

In the case of the author, you are using Ruby On Rails, can try this (as per 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).

There is this example too http://github.com/binarylogic/authlogic_example

  • I get it. there’s no way so just get the ok what to press and check whether it was true or false ? and then create something do logout then.

  • understood, is made in Ruby. = D

  • 1

    @ralfting to see if it can apply logica to Ror http://answall.com/a/71023/3635

  • Blz, I’ll test that. See what I can do and give feedback here.. Thanks for the help!

  • @ralfting Just do not help you with the ROR because my knowledge on this is limited and your question here is addressed to the problem Blocked confirm('Deseja fazer logout?') during beforeunload, but I await your feedback

  • Ta beauty would really prefer to treat this in html as it will be a user-triggered event.

  • @ralfting to facilitate I edited the answer and added an example in ROR, I hope it helps.

  • Thank you! I’ll see.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.