检测用户何时离开网页的最佳方法? [英] Best way to detect when a user leaves a web page?

查看:162
本文介绍了检测用户何时离开网页的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检测用户是否离开网页的最佳方法是什么?

What is the best way to detect if a user leaves a web page?

onunload JavaScript事件每次都不起作用(HTTP请求所需的时间比终止浏览器所需的时间长)。

The onunload JavaScript event doesn't work every time (the HTTP request takes longer than the time required to terminate the browser).

创建一个可能会被当前的浏览器阻止。

Creating one will probably be blocked by current browsers.

推荐答案

尝试 onbeforeunload 事件:在卸载页面之前触发它。它还允许您回询用户是否真的要离开。请参阅演示 onbeforeunload演示

Try the onbeforeunload event: It is fired just before the page is unloaded. It also allows you to ask back if the user really wants to leave. See the demo onbeforeunload Demo.

或者,您可以发送 Ajax 他离开时请求。

Alternatively, you can send out an Ajax request when he leaves.

这篇关于检测用户何时离开网页的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆