Capture the Browser Close event

Discussion in 'ASP .Net' started by =?Utf-8?B?QmlsbCBNYW5yaW5n?=, Mar 7, 2005.

  1. I need to capture the event when the user closes the browser in my
    application. I have some code in the session_End event, which works fine
    when the session times out, but I need to end the session immediately when
    the user closes the browser.

    Does anyone know a way of doing this?

    --
    Thanks,

    Bill Manring
    =?Utf-8?B?QmlsbCBNYW5yaW5n?=, Mar 7, 2005
    #1
    1. Advertising

  2. Hi Bill,

    This is a tricky issue. The odds of you getting a succesful postback are
    50/50
    to say the best. I've had about a 95% success rate doing this:

    1. Capture the onbeforeunload event in your body tag.
    2. Incorporate the WebService.htc.
    3. When the onbeforeunload event fires run a function that uses the
    WebService.htc to tell a web service.

    The browser will close before you get a response back but, about 95% of the
    time, your web service will get hit and the user can successfully be logged
    out. Note, that using the latest IE and Windows XP you will achieve almost
    100% results. This is sort of complicated until you get the hang of it. If
    this isn't something you want to try then here is another suggestion to
    improve your chances but it might seem clunky to the user:

    Have a main page that the user browses to. To run your app, they click a
    button or link and then you use window.open() to create a new browser
    window. This is the window your app runs from. Don't ask me why, but the
    success rate of a postback from onbeforeunload seems to be much higher for
    browsers launched from javascript than from browsers launched by the user.
    Good luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "Bill Manring" <> wrote in message
    news:...
    > I need to capture the event when the user closes the browser in my
    > application. I have some code in the session_End event, which works fine
    > when the session times out, but I need to end the session immediately when
    > the user closes the browser.
    >
    > Does anyone know a way of doing this?
    >
    > --
    > Thanks,
    >
    > Bill Manring
    >
    Ken Dopierala Jr., Mar 7, 2005
    #2
    1. Advertising

  3. Ken,

    Thanks for the response. It turns out that my application already works the
    way you suggested, with the app started from javascript, so your idea seems
    to work. Everything would be fine except for the fact that the Session_Start
    event seems to fire twice. I tried starting the main page directly, not
    using the javas script but the Session_Start event still fires twice.

    I am trying to keep track of the number of users logged in to our
    application for purposes of enforcing a "concurrent user" license model. I
    didn't want to wait until the session timed out to decrement the active user
    count, which I am storing in a database. If I rely on the Session_Start
    event to increment the user count, I am incrementing the count by two, which
    is of course not desirable.

    Any ideas on why the Session_Start event fires twice?

    "Ken Dopierala Jr." wrote:

    > Hi Bill,
    >
    > This is a tricky issue. The odds of you getting a succesful postback are
    > 50/50
    > to say the best. I've had about a 95% success rate doing this:
    >
    > 1. Capture the onbeforeunload event in your body tag.
    > 2. Incorporate the WebService.htc.
    > 3. When the onbeforeunload event fires run a function that uses the
    > WebService.htc to tell a web service.
    >
    > The browser will close before you get a response back but, about 95% of the
    > time, your web service will get hit and the user can successfully be logged
    > out. Note, that using the latest IE and Windows XP you will achieve almost
    > 100% results. This is sort of complicated until you get the hang of it. If
    > this isn't something you want to try then here is another suggestion to
    > improve your chances but it might seem clunky to the user:
    >
    > Have a main page that the user browses to. To run your app, they click a
    > button or link and then you use window.open() to create a new browser
    > window. This is the window your app runs from. Don't ask me why, but the
    > success rate of a postback from onbeforeunload seems to be much higher for
    > browsers launched from javascript than from browsers launched by the user.
    > Good luck! Ken.
    >
    > --
    > Ken Dopierala Jr.
    > For great ASP.Net web hosting try:
    > http://www.webhost4life.com/default.asp?refid=Spinlight
    > If you sign up under me and need help, email me.
    >
    > "Bill Manring" <> wrote in message
    > news:...
    > > I need to capture the event when the user closes the browser in my
    > > application. I have some code in the session_End event, which works fine
    > > when the session times out, but I need to end the session immediately when
    > > the user closes the browser.
    > >
    > > Does anyone know a way of doing this?
    > >
    > > --
    > > Thanks,
    > >
    > > Bill Manring
    > >

    >
    >
    >
    =?Utf-8?B?QmlsbCBNYW5yaW5n?=, Mar 8, 2005
    #3
  4. Hi Bill,

    Do your users need to log in? If so, are you using Forms or Windows
    authentication? If you do then you could update a date/time field in your
    database each time you authenticate them in the Application_BeginRequest.
    Then you could write a query to automatically set people as logged off when
    they are inactive for a set number of minutes. I have web apps that track
    online users like you but all of my apps use client-side scripting to
    constantly hit the server behind the scenes every couple of seconds to prove
    that they are still online. This requires javascript to be enabled in the
    browser. Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "Bill Manring" <> wrote in message
    news:...
    > Ken,
    >
    > Thanks for the response. It turns out that my application already works

    the
    > way you suggested, with the app started from javascript, so your idea

    seems
    > to work. Everything would be fine except for the fact that the

    Session_Start
    > event seems to fire twice. I tried starting the main page directly, not
    > using the javas script but the Session_Start event still fires twice.
    >
    > I am trying to keep track of the number of users logged in to our
    > application for purposes of enforcing a "concurrent user" license model.

    I
    > didn't want to wait until the session timed out to decrement the active

    user
    > count, which I am storing in a database. If I rely on the Session_Start
    > event to increment the user count, I am incrementing the count by two,

    which
    > is of course not desirable.
    >
    > Any ideas on why the Session_Start event fires twice?
    >
    > "Ken Dopierala Jr." wrote:
    >
    > > Hi Bill,
    > >
    > > This is a tricky issue. The odds of you getting a succesful postback

    are
    > > 50/50
    > > to say the best. I've had about a 95% success rate doing this:
    > >
    > > 1. Capture the onbeforeunload event in your body tag.
    > > 2. Incorporate the WebService.htc.
    > > 3. When the onbeforeunload event fires run a function that uses the
    > > WebService.htc to tell a web service.
    > >
    > > The browser will close before you get a response back but, about 95% of

    the
    > > time, your web service will get hit and the user can successfully be

    logged
    > > out. Note, that using the latest IE and Windows XP you will achieve

    almost
    > > 100% results. This is sort of complicated until you get the hang of it.

    If
    > > this isn't something you want to try then here is another suggestion to
    > > improve your chances but it might seem clunky to the user:
    > >
    > > Have a main page that the user browses to. To run your app, they click

    a
    > > button or link and then you use window.open() to create a new browser
    > > window. This is the window your app runs from. Don't ask me why, but

    the
    > > success rate of a postback from onbeforeunload seems to be much higher

    for
    > > browsers launched from javascript than from browsers launched by the

    user.
    > > Good luck! Ken.
    > >
    > > --
    > > Ken Dopierala Jr.
    > > For great ASP.Net web hosting try:
    > > http://www.webhost4life.com/default.asp?refid=Spinlight
    > > If you sign up under me and need help, email me.
    > >
    > > "Bill Manring" <> wrote in message
    > > news:...
    > > > I need to capture the event when the user closes the browser in my
    > > > application. I have some code in the session_End event, which works

    fine
    > > > when the session times out, but I need to end the session immediately

    when
    > > > the user closes the browser.
    > > >
    > > > Does anyone know a way of doing this?
    > > >
    > > > --
    > > > Thanks,
    > > >
    > > > Bill Manring
    > > >

    > >
    > >
    > >
    Ken Dopierala Jr., Mar 8, 2005
    #4
  5. Ken,

    The users do login, when the application is isntalled they decide whether
    they want to use Windows authentication or Forms authentication. I will
    consider another stategy - keeping a table of logged in users in the
    database. This might allow the possibility of users "sharng" a common login
    to circumvent the license restriction, but maybe I'll live with that. I'll
    give it some more thought.

    Thanks for your help.

    "Ken Dopierala Jr." wrote:

    > Hi Bill,
    >
    > Do your users need to log in? If so, are you using Forms or Windows
    > authentication? If you do then you could update a date/time field in your
    > database each time you authenticate them in the Application_BeginRequest.
    > Then you could write a query to automatically set people as logged off when
    > they are inactive for a set number of minutes. I have web apps that track
    > online users like you but all of my apps use client-side scripting to
    > constantly hit the server behind the scenes every couple of seconds to prove
    > that they are still online. This requires javascript to be enabled in the
    > browser. Ken.
    >
    > --
    > Ken Dopierala Jr.
    > For great ASP.Net web hosting try:
    > http://www.webhost4life.com/default.asp?refid=Spinlight
    > If you sign up under me and need help, email me.
    >
    > "Bill Manring" <> wrote in message
    > news:...
    > > Ken,
    > >
    > > Thanks for the response. It turns out that my application already works

    > the
    > > way you suggested, with the app started from javascript, so your idea

    > seems
    > > to work. Everything would be fine except for the fact that the

    > Session_Start
    > > event seems to fire twice. I tried starting the main page directly, not
    > > using the javas script but the Session_Start event still fires twice.
    > >
    > > I am trying to keep track of the number of users logged in to our
    > > application for purposes of enforcing a "concurrent user" license model.

    > I
    > > didn't want to wait until the session timed out to decrement the active

    > user
    > > count, which I am storing in a database. If I rely on the Session_Start
    > > event to increment the user count, I am incrementing the count by two,

    > which
    > > is of course not desirable.
    > >
    > > Any ideas on why the Session_Start event fires twice?
    > >
    > > "Ken Dopierala Jr." wrote:
    > >
    > > > Hi Bill,
    > > >
    > > > This is a tricky issue. The odds of you getting a succesful postback

    > are
    > > > 50/50
    > > > to say the best. I've had about a 95% success rate doing this:
    > > >
    > > > 1. Capture the onbeforeunload event in your body tag.
    > > > 2. Incorporate the WebService.htc.
    > > > 3. When the onbeforeunload event fires run a function that uses the
    > > > WebService.htc to tell a web service.
    > > >
    > > > The browser will close before you get a response back but, about 95% of

    > the
    > > > time, your web service will get hit and the user can successfully be

    > logged
    > > > out. Note, that using the latest IE and Windows XP you will achieve

    > almost
    > > > 100% results. This is sort of complicated until you get the hang of it.

    > If
    > > > this isn't something you want to try then here is another suggestion to
    > > > improve your chances but it might seem clunky to the user:
    > > >
    > > > Have a main page that the user browses to. To run your app, they click

    > a
    > > > button or link and then you use window.open() to create a new browser
    > > > window. This is the window your app runs from. Don't ask me why, but

    > the
    > > > success rate of a postback from onbeforeunload seems to be much higher

    > for
    > > > browsers launched from javascript than from browsers launched by the

    > user.
    > > > Good luck! Ken.
    > > >
    > > > --
    > > > Ken Dopierala Jr.
    > > > For great ASP.Net web hosting try:
    > > > http://www.webhost4life.com/default.asp?refid=Spinlight
    > > > If you sign up under me and need help, email me.
    > > >
    > > > "Bill Manring" <> wrote in message
    > > > news:...
    > > > > I need to capture the event when the user closes the browser in my
    > > > > application. I have some code in the session_End event, which works

    > fine
    > > > > when the session times out, but I need to end the session immediately

    > when
    > > > > the user closes the browser.
    > > > >
    > > > > Does anyone know a way of doing this?
    > > > >
    > > > > --
    > > > > Thanks,
    > > > >
    > > > > Bill Manring
    > > > >
    > > >
    > > >
    > > >

    >
    >
    >
    =?Utf-8?B?QmlsbCBNYW5yaW5n?=, Mar 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denon
    Replies:
    1
    Views:
    571
    Saravana [MVP]
    Nov 14, 2003
  2. feng
    Replies:
    4
    Views:
    11,782
    Eric Lawrence [MSFT]
    Feb 29, 2004
  3. =?Utf-8?B?RmVuZw==?=
    Replies:
    1
    Views:
    9,892
    Scott M.
    Nov 1, 2005
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    843
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Replies:
    0
    Views:
    490
Loading...

Share This Page