locked
window.onclose event ? RRS feed

  • Question

  • User-1670588033 posted

    Hi there,

     I need to save in my database the user's log in and log out time on my application.

    Its easy to do that when the user clicks on the LOG OUT link, that I've created.

     But, on the real world, a lot of users simply close the window by clicking on the X button, on the corner of the window.

    I wish I could capture that event, somehow, to save the current time.

     

    But it ain't such event as window.close.

    There is only window.unload, but thats fired every time the location changes also.

    Please help.

     

     

    Thanks

     

     

     

    Friday, August 24, 2007 8:25 AM

Answers

  • User-1670588033 posted

    Hi there,

    My friend came up with a JS solution that, in my opinion, is much better than the others...

    The idea is to create a frameset with a visible frame and a hidden frame.

    the master page and everything else goes on the visible frame

    So, the only way of raise the window.onunload event of the frameset page is by clicking on the window's X button.

    We tried this here and is working smoothly.

    Unfortunnaly the onunload event is not raised when the users clicks on Alt+F4 (I wonder why...) . We are working on that now...

     

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 28, 2007 10:06 AM

All replies

  • User923504599 posted

    This is a common problem, with no real great solution... 

    Here's an approach: http://blogs.x2line.com/al/archive/2004/09/15/561.aspx
    Here's another: http://www.codingforums.com/showthread.php?t=37279

    -Damien

    Friday, August 24, 2007 8:31 AM
  • User-1670588033 posted

    dwhite,

     I've seen the links you posted.

    You're write, none of them seemed to be a good solution.

    I wonder if I can use windows API to use that event.

     Im going to try it out and I'll let you know.

    But, of course, it still wouldn't be a good solution... 

    Thanks!

     

     

     

     

    Monday, August 27, 2007 12:38 PM
  • User923504599 posted

    You can't use the Windows API without getting control of the user's PC basically (e.g. ActiveX).  This wouldn't be viable for a public website. 

    I would stick with one of the standard JavaScript approaches.

    -Damien

    Monday, August 27, 2007 3:18 PM
  • User607120471 posted

    I would recommend not using JS to detect when the browser is closed. A big reason is that if they ever use CTRL-N to open a duplicate browser instance, they can close one instance which triggers your auto-logout notifier, however the cloned window will still have the same session ID in their non-persistent cookie store and if they re-navigate to the site, they will resume the same session on the server, but your system will already think they logged out.

    I would recommend using Session_End to timestamp this behavior. (assuming you aren't using web farm with distributed session state). Session_End is the closest to verfiable behavior since if their session times out, they will have to log in again. So if they leave the application open and never close it and 20 minutes go by, you consider them logged out already and when they come back to window and refresh, they have to re-login and create a new session. If you are using a load balancer, you can configure it to use session affinity to redirect requests to the same machine in the pool.

    This is primary method that banks use and they usually lower their session timeouts to about 2-3 minutes instead of the asp.net default of 20.

    If you must use a JS detection method, make sure the submit to the server from js ends up calling the server side "Session.Abandon" so that you avoid the user resuming a session that you think they already logged out of.

    -David

    Monday, August 27, 2007 4:38 PM
  • User-1670588033 posted

    I cant´t use session_end becouse the session life time is set to 4 hours.

    This is not a public web site. Its a company control system. I need to know when there is nobody logged in so I can throw updates. If I do that with someone logged in, the user will experience a very slow response untill the application re-compiles.

    Sometimes there are some users logged in but, since some users click on the browser X button to close the application, I will never know for sure if they are really there. So I have to stay at work untill very late at night to update the application.

    I think I will try the JS approach so my chances to go home earlier are bigger.

     Thanks

     

     

     

     

    Tuesday, August 28, 2007 8:19 AM
  • User-1670588033 posted

    Hi there,

    My friend came up with a JS solution that, in my opinion, is much better than the others...

    The idea is to create a frameset with a visible frame and a hidden frame.

    the master page and everything else goes on the visible frame

    So, the only way of raise the window.onunload event of the frameset page is by clicking on the window's X button.

    We tried this here and is working smoothly.

    Unfortunnaly the onunload event is not raised when the users clicks on Alt+F4 (I wonder why...) . We are working on that now...

     

    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 28, 2007 10:06 AM
  • User607120471 posted

    Well, i understand the constraints you are under, but I would recommend changing the session timeout to 5 minutes and deal with any issues surrounding that change than work with hacks that will not consistently be correct for the users.

    If the user force closes IE from process list, no events will be thrown and they will still be shown as online for hours. There are a number of other scenarios where the user will be disconnected without the js code from running and you will still have those sessions sitting around for 4 hours. (power fails at client location, system BSOD at client machine).

    If you are worried about the first-hit compilation time, then create a scheduled task to request a "keepalive.aspx" page from the application every 5 minutes to ensure that the app is always compiled and loaded. In the code-behind for the keepalive.aspx, when you want to run your update, temporarily disable the scheduled task, the sessions will drain in another 5 miuntes and you can perform your update.

    -David

    Tuesday, August 28, 2007 5:07 PM