 <p>This works perfect.</p> <p><a href="http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser">javascript detect browser close tab/close browser</a></p> <pre><code>&lt;body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()"&gt; var myclose = false; function ConfirmClose() { if (event.clientY &lt; 0) { event.returnValue = 'You have closed the browser. Do you want to logout from your application?'; setTimeout('myclose=false',10); myclose=true; } } function HandleOnClose() { if (myclose==true) { //the url of your logout page which invalidate session on logout location.replace('/contextpath/j_spring_security_logout') ; } } </code></pre>