[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1778
  • Last Modified:

how can we raise Session_End event when user closes the browser

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
0
mmalik15
Asked:
mmalik15
  • 5
  • 4
  • 2
1 Solution
 
slado2Commented:
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.
0
 
mmalik15Author Commented:
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
   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

any ideas  how can we achieve it

0
 
McExpCommented:
Please explain why you need to invoke session.end, So I may suggest an alternative solution?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mmalik15Author Commented:
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.
0
 
mmalik15Author Commented:
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)
           {
           
           }
           else
           {
      window.location.href = "TestB.aspx"
           
           }
     
                           }
             }

Any suggestions

0
 
McExpCommented:
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?
0
 
McExpCommented:
Was my last sugesstion acceptable or would you like me to look into alternative solutions?
0
 
mmalik15Author Commented:
thanks for asking
if you have an alternative solution that will be good
0
 
McExpCommented:
My final solution would be to use AJAX to callback to the server onUnload.

http://asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

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.
0
 
McExpCommented:
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?
0
 
slado2Commented:
As I said before. There's no 100% working solution. Imagine that the user just closes the lid of his notebook or there's electricity loss or network problem or browser hangs.... You won't get any notification at all.
In the web world the session rules. Do you really need to know immidiately that the user closed his browser? Why don't you just wait a few minutes until the session times out?
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now