'IE/Chrome unload event behavior: Refresh button vs. Selecting URL and pressing Enter

I have an event like this that logs the user out when they navigate away from my page:

$(window).unload(function () {
   $.ajax('/Account/LogOff', { async: false });
});

In IE, if the user hits the refresh button, they are logged out and when the page reloads, it detects that they are logged out and redirects them to the login page. This is a little bit annoying but acceptable behavior.

However if the user selects the URL in the address bar and presses Enter, the browser somehow starts loading the new page before the unload event and the user is logged out, but the user is nonetheless still logged out, and actions on my main page fail because the user no longer has an active session.

In Chrome, even using the refresh button is causing this to happen.

Is there any way to ensure that my unload event is fired before the new page starts to load?



Solution 1:[1]

No there is no way to do it. A few years ago it was possible to do ti with a while loop, but browsers have killed that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 epascarello