Click here to Skip to main content
15,748,930 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I want to clear my session once user close the browser however I am not able to get any events link onunload or onbeforeunload in javascript.
I have also tried to remove the session values in Session_End even in Global.ascx file which is also not firing .
Is there a way to catch the event when user close the browser or current tab?
sudevsu 19-Mar-15 15:35pm
nikhil choudhry 19-Mar-15 15:38pm    
Please don't give me the links if you have not tried them as I have already tried with body tag events and they are not firing.

sudevsu 19-Mar-15 15:51pm    
Ok Nihkil Post what you have tried. Based on that I will try to help you
jgakenhe 20-Mar-15 0:26am    
Thanks for the link. It's getting a little late for me, but it looks like a promising solution.
sudevsu 23-Mar-15 8:52am    
Good to know that it helped you.

There is no full-proof technique, but here is some probable solutions ...

When a user closes the browser, the session doesn't literally expire for a default time of 20 minutes. When the session expires the OnEnd event fires. You can code against this in the global.asax file like so ...

Sub Session_OnEnd()
    Dim conn As New SqlConnection(CONNECT_STRING)
    Dim cmd As New SqlCommand( _
        "insert into _DummyTbl (Text) values ('GLOBAL')", conn)

End Sub

Now if you call the Session.Abandon method the OnEnd event will fire immediately. (NOT quite though, because I noticed that it took up to a few minutes at certain times for a record to appear into the database, but this is the best we are going to get.) Now we need some way to call the Session.Abandon method when a user closes their browser. This trick wont work in all web browsers, but you can tap into the onunload() event like so ... TESTA.ASPX
<body onunload="window.location.href="/KB/answers/TestB.aspx";">

When the user closes the browser window at TestA.aspx, they are redirected to TestB.aspx ... TESTB.ASPX
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

On page load event of TestB.aspx, the code abandons the session then closes the browser immediately. The user never sees this page. In fact to the user, it looks like they closed TestA.aspx.

This javascript technique speeds up the process of killing the session and calling Session_OnEnd, but is not supported on all browsers. For older browsers, the application is just gonna have to suffer the 20 minute session timeout period.

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900