0

Possible Duplicate:
javascript detect browser close tab/close browser

Hello guys,

I have problem to detect the session end when the browser is closed, how can i get this, i want to fire a query to store the login time information in database, please help me. Thanks to all

Community
  • 1
  • 1
Abhishek
  • 913
  • 3
  • 19
  • 42

2 Answers2

3

It is highly unreliable to rely on that, An ajax query may not be completed on browser close.

Solution:

Sending a heartbeat request every x seconds in order to tell if the user left or not.

Steven Sudit
  • 19,045
  • 1
  • 48
  • 52
CodeOverload
  • 45,400
  • 52
  • 128
  • 215
  • 1
    The very last thing we need on the Internet is websites abusing AJAX for "heartbeat" requests. – user229044 Feb 03 '11 at 13:35
  • 1
    @meagar i agree, i don't know why the asker exactly want to sign up the user on browser close rather than the traditional way. i thought he was building a real time analytics service.. – CodeOverload Feb 04 '11 at 21:05
1

You could create a hidden button and then fire the button click event using

function doUnload()
{
 if (window.event.clientX < 0 && window.event.clientY < 0)
 {
   document.getElementByID('HiddenButton').click() }
}
<body onunload="doUnload()">

Never tried it but it might work :)

or you could use the global.aspx methods

    protected void Session_End(object sender, EventArgs e)
    {

do something; }

    protected void Application_End(object sender, EventArgs e)
    {

do something; }

Sp

Steven
  • 2,078
  • 8
  • 33
  • 50
  • -1 Application_End is not directly related to the client activity and Session_End is not reliable because it's fired after an amount of time session has been inactive – onof Feb 03 '11 at 11:27
  • I suppose it depends how accurate they require it to be. just thought id throw it out there. :) – Steven Feb 03 '11 at 11:38