When ever user leaves chat page (Either by logging out or just by simple closing the browser window). Chat script instantly detects that user left and show offline sign. Thi
Just have your JavaScript chat code send a message to the server every 2 seconds using XMLHttpRequest. When you don't receive a message, that means that the user has closed the window.