有没有办法检测用户是否按下“留在页面”或“离开页面”在beforeunload事件? [英] Is there any way to detect if user pressed "Stay on page" or "Leave page" in beforeunload event?
本文介绍了有没有办法检测用户是否按下“留在页面”或“离开页面”在beforeunload事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以在以下代码中检测到:用户点击离开页面或留在页面按钮? code> $(window).on('beforeunload',function(){
return你保存一些未保存的数据,你想离开吗?;
});
解决方案
使用几个黑客,至少可以确定用户停留了。
如果用户离开了网页,您可以做的不多:
var timeout;
$(window).on('beforeunload',function(){
timeout = setTimeout(function(){
// user stayed,do stuff here
},1000);
返回您保存一些未保存的数据,是否要离开?;
});
Is there any way that I could detect in the following code that either user clicked "leave page" or "stay on page" button ?
$(window).on('beforeunload', function (){
return "You save some unsaved data, Do you want to leave?";
});
解决方案
With a few hacks you can at least determine if the user stayed.
If the user left the page, there's not much you can do about that :
var timeout;
$(window).on('beforeunload', function (){
timeout = setTimeout(function() {
// user stayed, do stuff here
}, 1000);
return "You save some unsaved data, Do you want to leave?";
});
这篇关于有没有办法检测用户是否按下“留在页面”或“离开页面”在beforeunload事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文