1

I am building a chat room and I have to determine whether a user is online or not. So when a user logs in, I update his status as online and when he logs out, I update his status as offline, but when the user simply closes the browser, the status remains online.

To fix this I need to execute a server side code on the event of browser termination. How do I do this?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • possible dup http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser – Ray Cheng Nov 25 '12 at 07:58

1 Answers1

0

I believe this is what you are looking for.

When a user closes a browser window, the unload event is triggered. You could trigger an ajax request on this event that posts to the server that the user has closed his/her window.

window.onunload = function(){
    // make an ajax request that notifies the 
    // server that user has closed the window
}
AndyPerlitch
  • 4,539
  • 5
  • 28
  • 43