Window OnClose() event Javascript

Moe Tarhini

Newcomer
Joined
Jul 9, 2003
Messages
15
Hi All,
I have an urgent task. I want to log out users automatically when they hit the X button or close the window.
I am using Body OnUnload event , but this event is firing also when submitting or Reloading.
Is there a way to distinguish between unload an Close events.
Thank you.
Moe Tarhini
 
ok all what you have to do is to write this code in the window_onbeforeunload event
var iX = window.document.body.offsetWidth - window.event.clientX ;
var iY = window.event.clientY ;

if (iX <=30 && iY < 0 )
{
// this means the user clicked the X button, do whatever you want to do here
}

ur lucky man bc i fell into this problem yesterday and it took me the whole day to solve it. i guess u will also need to know if the user presses the alt f4 because this will also close the window so ull need also to write code to handle this.
nb: mehyar yahfoufi says hi
 
Thank you very much for this tip! I was roaming the net trying to solve a similar probleme when I found your post..

It works very well.

Thanks a lot!
 
Back
Top