检测用户何时离开网页的最佳方法?如果用户离开网页,什么是最好的检测方法?这个onunloadJavaScript事件并不是每次都能工作(HTTP请求比终止浏览器所需的时间更长)。创建一个可能会被当前的浏览器阻止。
3 回答

米脂
TA贡献1836条经验 获得超3个赞
window.addEventListener('beforeunload', function(e) { var myPageIsDirty = ...; //you implement this logic... if(myPageIsDirty) { //following two lines will cause the browser to ask the user if they //want to leave. The text of this dialog is controlled by the browser. e.preventDefault(); //per the standard e.returnValue = ''; //required for Chrome } //else: user is allowed to leave without a warning dialog});
添加回答
举报
0/150
提交
取消