5

有没有办法实时判断我网站上的用户是否离开(即连接断开、导航离开等)?

我正在建立一个 RoR 网站,并希望在用户出于任何原因离开我的网站时立即发起一个事件(例如连接、离开域等)

提前感谢您的帮助。

4

4 回答 4

6

No, there isn't.

The best you can do is send an AJAX request every X seconds (perhaps only if the user moves the mouse). If the server doesn't receive any requests for 2X seconds, assume that the user left.

于 2010-07-09T18:52:46.390 回答
2

Nope.

Http = connectionless

于 2010-07-09T18:54:58.230 回答
2

你可以在这个函数中做一些请求:

$(window).bind('beforeunload', function(){ 
  // there
});

发送一些 Ajax 或其他东西来告诉服务器该用户正在离开页面,但这不是一个好主意。在 Google 分析和其他统计服务中,您已经可以看到用户在您的页面中停留的时间。

于 2010-07-09T18:58:16.843 回答
0

All I can think of is JavaScript onunload event in your body tag... I don't know how viable that is, but in theory you could have that use an AJAX call to send information about an impending disconnect.

Of course, if the browser process is just killed that won't work.

And I'm not sure if Rails has any unique magic that does this same thing (except better).

于 2010-07-09T18:52:24.033 回答