View Full Version : Log Out of system when close browser window

07-11-2011, 09:26 AM
Hi folks,

I've got a chat program which relies on a column in a MySQL database to see if he/she has logged out or not. But if you close the browser window, the computer still thinks you're online. I wrote this in the JavaScript section cos I think it would definitely need Javascript. So any help for updating the database in any way? Thanks!!

:thumbsup: :) :D :cool:

07-11-2011, 10:56 AM
Use a session cookie for that.

07-22-2011, 09:25 AM

but the main problem is that using the computer you can't find if a person if offline or online because session variables are only in the computer, not accessible by another person's computer. That's why i made a loggedin column in my database, cos databases are accessible everywhere.

07-22-2011, 10:18 AM
You can add/attach an event listener for "onbeforeunload" to the window object and do whatever you want to do in the callback. You can even give a string as a return value. This string will be displayed to the user together with two buttons. That will give the user the choice to stay on the page or to leave it (no other options possible).

window.onbeforeunload = myExitFunction;

function myExitFunction() {
// your actions here
return "This string will be shown in a message box";

07-24-2011, 10:39 AM
can jQuery ajax be performed in the window.onbeforeunload event as well?

07-24-2011, 01:45 PM
Just try ...