0

I need to call a particular JS function when the user exits the Google Chrome browser. I wrote a simple code:

window.onunload = logout;

    function logout()
    {
        window.location = "auth/logout";
    }

This code works on other browser but not on Chrome. It is essential for me to bring this functionality to Chrome too because the web browser has a large user base. How could I apply the functionality of the above code to all browsers including Chrome? Alternately, a workaround for Chrome would also be very helpful.

Mayank Choudhary
  • 376
  • 2
  • 7
  • 18
  • 1
    The `onunload` and `onbeforeunload` events are notoriously unreliable, and building functionality that totally depends on those events is a horrible idea. Also, redirecting when the user is closing the browser is always a horrible idea, you have no idea if the browser was able to redirect or not before it closed, it's all dependent on the browser, connection speed, and probably other things, and you can't stop the user from closing the browser, that would be really annoying. – adeneo Dec 09 '15 at 07:16
  • @adeneo: You got any other ideas? I need to log the user out if in case he closes the browser before logging out manually. – Mayank Choudhary Dec 09 '15 at 07:29

1 Answers1

0

IMHO, it's never good UX to open a new window when a user tries to close a window. It's annoying. And as has been stated, even if you could make it work in most cases, this method would be unreliable and ill-advised.

You'd be much better served using one of the below to create the functionality you need:

PHP Sessions

Browser Session Cookies

Wesley Smith
  • 19,401
  • 22
  • 85
  • 133