We help IT Professionals succeed at work.

how can we raise Session_End event when user closes the browser

mmalik15 asked
Last Modified: 2013-11-26
i am using a Master page application & i am able to raise the Session_End event in Global.asax after user session is expired but i want to fire up the event if user closes the browser. I am using vb.net with framework 2.0
Watch Question

The only chance is some Javascript that would call your handler via XmlHttpRequest object, but generally there's no reliable way to react when the user closes the browser.


Thanks for your comment.
I am not very good at java script. i have tried one of the script that does not seem to work. In my master page body element i added
<body onunload="window.location.href='TestB.aspx';">
and on  my TestB page i have following code

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   Call MyBase.OnLoad(e)
   ' End The Session

   ' Build A JavaScript String That Will Close This Web Browser Window
   Dim s As String = ""
   s &= "<script language=""javascript"">"
   s &= "window.close();"
   s &= "</script>"
   ' Add The JavaScript To The HTML Stream
   Page.RegisterClientScriptBlock("close", s)
End Sub

any ideas  how can we achieve it

Top Expert 2007

Please explain why you need to invoke session.end, So I may suggest an alternative solution?


when a user logins to the application i get and store the start time and on session end i get the log off time. Now if some one closes the browser i need to log user off and get the log off time.


What i have done now is to add a confirm message box using javascript when user clicks the close button and i want to redirect the user to another page (using  window.location.href = "Test.aspx") which will call session.abondon. Butt the problem that i am facing in using that is when  user clicks ok on the confirm message box we cannot redirect to another page coz perhaps page is already unloaded
Javascript in my master page

<body id="htmlBody" runat="server" onbeforeunload="HandleOnClose()">

function HandleOnClose()
    if (event.clientY < 0) {
      var result=event.returnValue = 'Are you sure you want to leave the page?';
           if (result)
      window.location.href = "TestB.aspx"

Any suggestions

Top Expert 2007

A simple solution would be to reduce the Session time to 5 minutes, then you would find automatically that Session times are correct to within 5 minutes?
Top Expert 2007

Was my last sugesstion acceptable or would you like me to look into alternative solutions?


thanks for asking
if you have an alternative solution that will be good
Top Expert 2007

My final solution would be to use AJAX to callback to the server onUnload.


Word of caution HTTP is Stateless and you cannot gauarentee that you will get this event to fire whatever technology you use. If your attempting to call a server side method as part of the client side dom unload event I would seriously encourage you to abandon this endeavour. You can not reliably capture this event. If i hit Alt+F4 in the browser the browser will close without sending the event to the server, thus you can't depend on the event triggering some server side code.
Top Expert 2007

My bonus solution would be to abandon using Sesson_End to try and capture the end and add functionallity to your pages to update a "Last Activity time" for the current user. This could be done in a master page or a HttpModule, or other?

You would then use the session_end to take the value you recorded to the LastActivityTime and save as your session end.

This has only one drawback in that the Session end would represent the time they started looking at the final page not the time they stopped looking.

Does that make sense?
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.