如何判断用户是否离开了我的网站
有没有办法实时判断我网站上的用户是否已离开(即连接断开、导航离开等)?
我正在构建一个 RoR 网站,并且希望在用户出于任何原因(例如连接、离开域等)离开我的网站时立即启动一个事件。
提前感谢您的帮助。
Is there a way to tell if a user on my website has left (i.e. connection dropped, navigated away etc.) in real time?
I am building a RoR website and want to initiate an event as soon as a user leaves my site for whatever reason (e.g. connection, navigating away from domain etc.)
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,没有。
您能做的最好的事情就是每 X 秒发送一个 AJAX 请求(也许仅当用户移动鼠标时)。如果服务器在 2X 秒内没有收到任何请求,则假设用户已离开。
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.
没有。
HTTP = 无连接
Nope.
Http = connectionless
你可以在这个函数中做一些请求:
发送一些Ajax或者其他东西来告诉服务器这个用户正在离开页面,但这不是一个好主意。在谷歌分析和其他统计服务中,您已经可以看到用户在您的页面上停留的时间。
You can do some request in this function:
Send some Ajax or something to tell the server that this user is leaving the page, but this is not a good idea. In Google analytics and in other statistic services you already can see how long the user stays in your page.
我能想到的就是你的 body 标记中的 JavaScript
onunload
事件...我不知道这有多可行,但理论上你可以使用 AJAX 调用来发送有关即将发生的信息断开。当然,如果浏览器进程只是被杀死,那是行不通的。
我不确定 Rails 是否有任何独特的魔力可以完成同样的事情(除了更好)。
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).