Below javascript is use to detect wether browser is refereshed or closed.
window.onunload = function (e) { // Firefox || IE e = e || window.event; var y = e.pageY || e.clientY; if (y < 0) { alert("close"); } else { alert("refresh"); } }
Thanks.
Posted by Viral Sarvaiya on May 8, 2012
Below javascript is use to detect wether browser is refereshed or closed.
window.onunload = function (e) { // Firefox || IE e = e || window.event; var y = e.pageY || e.clientY; if (y < 0) { alert("close"); } else { alert("refresh"); } }
Thanks.
Posted in ASP.NET, Javascript, Silverlight | Tagged: Browser, Browser Evens, close event, F5, Referesh Browser, Referesh Event | 2 Comments »
Posted by Viral Sarvaiya on April 12, 2011
hi,
recently i get a problem that what if some user accessing my website and close directly browser without logout from the system?
i get good solution with this but this is not 100% secure.
for that we have 2 events in javascript “onbeforeunload” or “onunload”
but onunload event is not working in firefox and some time in IE8 also, so onbeforeunload is suitable for both.
<html> <head> <title>Window Close Event of Browser</title> <script language="javascript" type="text/javascript"> function CloseBrowser() { alert("Browser is closing"); } </script> </head> <body onbeforeunload="CloseBrowser();"> </body> </html>
window.onbeforeunload = function() { return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse... };
As we have already discussed that there is no 100% fool proof way to detect the browser close event in a lot of cases, the above technique can fail. For example if the user kills the browser’s process from task manager, there could be many more cases. Yet in a lot of cases, this technique can be handy.
Best of luck and happy programming.
Thank you.
Posted in ASP.NET, Javascript | Tagged: close event, Firefox, IE, onbeforeunload, window close event, Window Close Event of Browser | Leave a Comment »