有没有办法在下面的代码中检测到用户点击了“离开页面”或“留在页面上”按钮?
$(window).on('beforeunload',function (){
return "You save some unsaved data,Do you want to leave?";
});
最佳答案
通过几次黑客攻击,您至少可以确定
用户是否留下来.
如果
用户离开了
页面,那么你无能为力:
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?";
});
FIDDLE
原文链接:https://www.f2er.com/jquery/428256.html