简体   繁体   中英

How can I know the user closes tab or browser in html?

I´m working on a webapp MVC and want to know when the user closes the browser tab, in order to destroy his session. I´m working with jsp (html, js) and java for the internal code. Some ideas?

You can use window.onunload function from javascript

window.unload = function () {
  //doSomething
}

or add an eventlistener to the unload event of the window in following way:

window.addEventListener('unload', function() {
  // doSomething
})";

Have a look at the full docs on more details onunload and unload event.

You can use this link and put your ajax session destroy there -> Run JavaScript code on window close or page refresh

after the Ajax call, you can easily get the session and invalidate/remove it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM