Click here to Skip to main content
14,929,655 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?
Posted
Comments
sudevsu 19-Mar-15 15:35pm
   
http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed
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.

Thanks
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.
ZurdoDev 19-Mar-15 16:03pm
   
In document.ready (using jQuery) wire up window.onbeforeunload = ClearSession;

Then in ClearSession you call a webservice that clears out what you want. However, there is no guarantee that this will work. Users computers can crash, they can kill the browser, they can have your app open in 2 tabs and closing one will kill both.

Basically, there is no perfect way of doing this.
sudevsu 19-Mar-15 16:07pm
   
Hey by the way, when browser is close usually session will be cleared. Did you notice anything still having value?
Modi Mayank 19-Mar-15 23:41pm
   
I'm also agree with "sudevsu", when you close the browser, session will automatically cleared. It's by default behavior of any browser.

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 ...

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

    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()
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
VB
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   Call MyBase.OnLoad(e)

   ' End The Session
   Session.Abandon()

   ' 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
   

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