Click here to Skip to main content
15,908,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys i needed to call a function when user clicks on browser close buton/closing of browser, i have tried many things like
1)In body tag OnUnload() but it runs every time when post back and its not working
i also tried to catch Browser close button
2)
function doUnload()
{
 if (window.event.clientX < 0 && window.event.clientY < 0)
 {
   alert("Window is closing...");
 }
}  its also not working
...
<body  önunload="doUnload()">





so any trick or logic will be highly apreciated

Thanks
Posted

if u give in unload function it will not work in IE browser...
for that give that function in "onbeforeunload" like....


<body onbeforeunload="javascript:alert('window is closing...');">
 
Share this answer
 
I see two possible problems with your code.

I believe that IE returns null if the mouse is outside the browser's client window when the event fires. Your conditional would therefore evaluate to false.

Also, if you are closing the browser by clicking on the Exit button, then window.event.clientX would be a positive value, as you are to the right of the 0, 0 point.
 
Share this answer
 
hi,


Try this lines for that.

JavaScript
  $(document).ready=function(){
    window.onunload=function(){alert("dfgfdg");};
};



I hope it works for you also


All the best
 
Share this answer
 
give with out if condition....


like below..



C#
function doUnload()
{
   alert("Window is closing...");
}
 
Share this answer
 

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