Reputation: 31
I have web application workung with master page. I want to know when the user is closing the browser' then I'll raise event to clean' session variables. How can I detect the browser closing/ I tried the unload jscript event' but it fired when I move from page to page. Anyone has an idea?
Upvotes: 2
Views: 1615
Reputation: 47183
Use an AJAX "heartbeat". That is, pool the server every 30 seconds or so using Javascript. If you don't recieve a response on your server in, let's say, one and a half minute, you can be pretty sure that browser window is closed. Unless your user disabled Javascript in the meantime...
Upvotes: 1
Reputation:
Try this: (This is what we did in our project, Only 99% successful)
// Java Script Start
window.onbeforeunload = function(e) {
try {
e = e || window.event;
// add any of your logic here.
}
catch (ex) {
alert(ex.message);
}
}
//Script End
Just add this script anywhere on your page. its a windows event, which gets called "Before the window is closed". Please avoid doing time-intensive work here, that might lead to hang the i.e or worst crash it!
Regards,
Mazhar Karimi
Upvotes: 1