views:

173

answers:

3

I am facing an issue in my application when a user directly clicked on browser close [X] button. Browser can be IE, Chrome, Mozilla, Firefox and many more.

What I want to do: 1. as soon as user hits [X] button of browser, need to set their status as logged off in database for which we have a method in Login.aspx file which is within the master page. 2. We do not have any Logoff feature in the application

I will be thankful if anyone suggests a solution to call the method which sets the user status as logged off from master page.

Thanks in advance.

+3  A: 

This is not possible due to the nature of http connections and the web in general. Simply have a timeout (eg. 10 minutes) after which a user gets logged out automatically.

Femaref
Moreover, JavaScript can be turned off (eventually during page visits) and nothing will be executed on the client side.
Marcel Korpel
+1  A: 

Javascript has an onunload function, so you could do:

<body onUnload="doFunction()">

However this, and other methods are going to be unreliable (I'm not sure in which specific instances it is fired) as it would be a security concern allowing websites to have access to perform many functions on browser onunload.

The best solution would be to have cookies/sessions automatically time out, and also to educate users to logout if the system is sensitive.

Tom Gullen
You would have to test for each browser the behaviour of the onunload function, and when it is fired you can submit an ajax request, or have a popup window which auto closes once it has performed its required logout script. I think the best way would be a popup, as the onunload function probably wont wait for the ajax request to complete before closing the window.
Tom Gullen
@Tom Thanks for suggestion, could you please give some codes for the same?
Rick
Definitely can't open a popup (as in a browser window, I think `alert()` works), pretty sure you can't AJAX either - maybe dumb POST works?
Simon Buchan
I've seen plenty of websites do popunders/popups when onunload is called, a while back I wrote an auto logout script which popped up a small window that ran some logout code, but again this solution would suffer functionality from popup blockers.@Gaurav, here's some exit popup scripts I found doing a google searchhttp://www.nowsell.com/pop-ups/exit-popup-scripts.html
Tom Gullen
Wether browserside solution is used, they can fail. So you always need something like a session timeout.
Femaref
on(before)unload doesn't fire in Opera, as this is very non-standard.
Marcel Korpel
As I have mentioned in the solution, there are a lot of stages this can fail at, including but not limited to: - Different behaviours of onunload in different browsers - Browsers not supporting onunload - Popup blockers on browser
Tom Gullen
I'm new to stack overflow, but why does my question not have any ratings when the answer above has 3 ratings even though it is incorrect? (Saying it is not possible is wrong, it is possible but with a lot of limitations).
Tom Gullen
Perhaps because this solution is not fail-safe, as you said. You simply cannot rely on it if logging off is somehow necessary.
Marcel Korpel
@Tom I called the method 'onUnload' from Master Page But the problem is it calls every time whenever page unloading, it calls eerytime whenever user move to another page or click on any link which redirect to another page.I will rate your answers, actually the same is not working.
Rick
A: 

If you are using jQuery you could work with

$(window).unload( function () {
  $.ajax({ **your params** });
} );

But I have to agree with Tom Gullen here - your sessions should timeout eventually.

Christina Mayers
Seriously, why bring jQuery into this? `window.onunload = function(){};` works just fine.
Sean Kinsey
I didn't say he should _add_ jQuery just for this unload - if he is using it already though, why not? It wont be any slower in any way.
Christina Mayers
In fact, it will, but only very minimal (I doubt a visitor will notice it): those `$` functions of jQuery have to be parsed using JavaScript (which is not *very* fast). The native function is as short as possible.
Marcel Korpel