Notify node.js when user leave the page

314 views
Skip to first unread message

Sunil Khedar

unread,
Apr 13, 2010, 1:43:40 AM4/13/10
to nodejs
Hi All,

I have tried for "unload" and "beforeunload" binding in following
code, but none of the browser, except FF, is sending a request to
node.js:

$(window).bind('unload', function(){
$.ajax({cache: false,
dataType: "script",
async: false,
url: "http://node.example.com/unsubscribe"
});
});

Any suggestion, how to notify node.js that user is leaving the page?

Thank you,
Sunil Khedar

Marak Squires

unread,
Apr 13, 2010, 1:55:46 AM4/13/10
to nod...@googlegroups.com
It should be noted that the unload event is never guaranteed to fire in the browser.


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


r...@tinyclouds.org

unread,
Apr 13, 2010, 2:30:23 AM4/13/10
to nod...@googlegroups.com

Add a listener for the 'close' event on request.connection?

Aaron Heckmann

unread,
Apr 13, 2010, 7:41:54 AM4/13/10
to nod...@googlegroups.com
I'd definitely use onbeforeunload. I wonder if just creating the
request with an img does the trick?

(new Image()).src = "node.example.com/die"

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>
>

--
Sent from my mobile device

Aaron

Connor Dunn

unread,
Apr 13, 2010, 8:03:17 AM4/13/10
to nod...@googlegroups.com
> I'd definitely use onbeforeunload. I wonder if just creating the
> request with an img does the trick?
>
> (new Image()).src = "node.example.com/die"

In my experience onbeforeunload with a jQuery ajax request works most
of the time in IE7 (not tested other IEs), Firefox and Chrome, it
doesn't always work though.

Miguel Cavaco Coquet

unread,
Apr 13, 2010, 8:23:09 AM4/13/10
to nod...@googlegroups.com
I cant be 100% sure now, but if i recall correctly, to use
onbeforeunload or onunload you need to stop the event before you do
anything, or it wil kill your ajax request before its done. That in
turn will probably present you with a nice browser popup thing.

My suggestion would be to handle this on the server if possible.

Good luck :)

Connor Dunn

unread,
Apr 13, 2010, 8:34:28 AM4/13/10
to nod...@googlegroups.com
> I cant be 100% sure now, but if i recall correctly, to use onbeforeunload or
> onunload you need to stop the event before you do anything, or it wil kill
> your ajax request before its done. That in turn will probably present you
> with a nice browser popup thing.

You don't need to stop the event for the ajax request to fire.

Miguel Cavaco Coquet

unread,
Apr 13, 2010, 8:47:46 AM4/13/10
to nod...@googlegroups.com
as long as it gets executed quickly, your right :) If its buried in
code, it might not. It should work, but its rather flaky.

Chris Winberry

unread,
Apr 13, 2010, 8:58:49 AM4/13/10
to nod...@googlegroups.com

Does the current page maintain a comet-like connection to the server? If so, then when the browser leaves the page it is going to cleanly shut down the connection (hopefully, otherwise you have to wait 30 secs for a timeout) which will be marked by a close event at the TCP level, rather than the HTTP level. Ryan has already mentioned this; what you're trying trying to do is more complicated and less reliable.

On Apr 13, 2010 8:47 AM, "Miguel Cavaco Coquet" <mco...@gmail.com> wrote:

as long as it gets executed quickly, your right :) If its buried in code, it might not. It should work, but its rather flaky.



On 2010/04/13, at 13:34, Connor Dunn wrote:

>> I cant be 100% sure now, but if i recall correctly...

Reply all
Reply to author
Forward
0 new messages