Skip to main content
Participating Frequently
April 14, 2011
Answered

Execute code when IE Browser closes

  • April 14, 2011
  • 1 reply
  • 650 views

Help!  I'm trying to find a way to run a SQL statement when a user closes a IE window (X's out).  I've found information on the onBeforeUnload event but I've been unsucessful at getting it to run.

Does anyone have another suggestion?  Is it even possible to execute code upon closing a window?

Thanks!!!

    This topic has been closed for replies.
    Correct answer ilssac

    You should be able to make use of the OnSessionEnd event.

    It won't happen immediatly, but it will happen when ColdFusion has determined to end the session.

    You can controll how long that is with parrameters, such as sessionTimeOut in either a <cfapplicaiton....> tag or this scope properties of an Application.cfc file.  Which you would be using anyways for the OnSessionEnd event handler.

    1 reply

    ilssac
    Inspiring
    April 14, 2011

    It is NOT possible to reliably have server side code execute when the client is closed.

    That is the nature of web applications.  The clients and the servers are not connected to each other.  Clients make a request, servers respond and then they forget about each other until another request is made.

    What is the REASON for this requirement?  How accurate does it have to be about when the user stopped making requests?

    There is the OnSessionEnd event in a ColdFusion Application.cfc framework.  But this event is not going to happen until the session time out occurs (20 minutes by default) after the last request from a given client.

    There are some tricks that can be attempted with JavaScript creating a heart beat request.  But that has all the caveats and concerns standard with JavaScript.

    Participating Frequently
    April 14, 2011

    Thanks ilssac.  I figured this was the case based on my research but I thought I'd present it to the Forum just to be sure.

    Basically, I've been asked to limit the number of logins to a small Coldfusion application.  I've coded it so that I increment a counter in the DB and I decrement it when the user logs out properly (by clicking a logout link).  But if the user just closes the window then the decrement doesn't happen and this created problems.

    If you have any suggestions I would really appreciate it.

    Thanks for your time!!!!

    ilssac
    ilssacCorrect answer
    Inspiring
    April 14, 2011

    You should be able to make use of the OnSessionEnd event.

    It won't happen immediatly, but it will happen when ColdFusion has determined to end the session.

    You can controll how long that is with parrameters, such as sessionTimeOut in either a <cfapplicaiton....> tag or this scope properties of an Application.cfc file.  Which you would be using anyways for the OnSessionEnd event handler.