Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

how to write code on browser close button event .. i want to log out user and change his status in Database even if he hit the close button of the browser.


Thanks in adv
Posted

http://forums.asp.net/t/1153888.aspx
http://www.codeproject.com/KB/cs/detect_closing_navigator.aspx
http://forums.asp.net/t/1318676.aspx
 
Share this answer
 
Comments
anvas kuttan 30-Apr-11 7:44am    
dheeraj .. Thank you for your help... i tried a lot ... i git the page close event and all but that JavaScript calling on the unload event of the body tag so it will arise even if there is refresh or a post back
There are two events

1) window.onbeforeunload
2)
<body onunload.</code><br />
check out which one suits you..
 
Share this answer
 
v4
Comments
anvas kuttan 2-May-11 5:40am    
dear thank you for your replay

i already gone through almost every way.. u know these events fire not only with close but also with refresh, postback everything. so i need close button specific
try this code..
this only fire on referesh and browser close...not on any postback

C#
<script type="text/javascript" language="javascript">
window.onbeforeunload = function(event)
{
var msg = 'Are you sure you want to leave?';

if (((window.event.clientX || event.clientX) < 0) || ((window.event.clientY || event.clientY)<0)) // close button
{
event=window.event;
event.returnValue = msg;
 return msg;

}
if ((window.event.clientX < 0) || (window.event.clientY<0)) // close button
{
event=window.event;
event.returnValue = msg;
 return msg;

}
else if (window.event.altKey == true || window.event.ctrlKey == true) // ALT + F4
{
event=window.event;
event.returnValue = msg;
 return msg;
}
else // for all other unload events
{
   
}

}
</script>
 
Share this answer
 
Comments
anvas kuttan 2-May-11 6:04am    
hi ashimau ,

thank you for your time

it is not working for me i just copy this code to a new window is i do anything more .. or i am working in firefox . plz help me .. is there any idea to avoid work this code on close button only i mean no refresh
Naeem Raza 9-Feb-12 4:36am    
its working for me.
Thanks
Vivek Deshmukh 13-Mar-12 6:22am    
Its working only for IE
RonRox 3-Aug-12 7:14am    
Thanks
Working for me too.
JituV10 10-Jul-14 2:29am    
Is it working in Chrome?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900