Skip to content Skip to sidebar Skip to footer

Automatic Log Out When Browser Closes

Is it possible to give a condition where as soon as you hit the close button of your browser, the script runs ? I have a log in page provided by my ISP, so I was wondering if it is

Solution 1:

If you work with cookies just don't set an expiration. So the Cookie will be deleted if you close the browser. With Javascript you could try to use the onunload or onbeforeunload event.

JS:

window.onunload = function() {
    document.cookie = 'mycookie=myvalue; expires=Thu, 01-Jan-70 00:00:01 GMT;';
};

or maybe you could send a last XHR request in the unload function. But i never tried it so i can't say if it is really going to work.


Solution 2:

If you want to be extra careful, or if you have interactive elements that need proper cleanup, you should use on of the unload events:


Solution 3:

You can bind the window.onunload event in javascript, possibly to call a PHP script via ajax, but this would be highly unreliable. Whenever the browser shut down ungracefully, for example, the script would not fire. Also it would not fire if javascript were disabled.


Solution 4:

Better reduce the time until a session expires and refresh the session by asynchronly calling a page (via a xhr) which will extend the session.


Post a Comment for "Automatic Log Out When Browser Closes"