在下面的代码中,有没有任何方法可以检测到用户单击了"离开页面"或"停留在页面上"按钮?
$(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
iirc,当用户决定留在页面上时,不会触发卸载事件。所以:
$(window).on('unload', function (){
// user has left the page, do stuff here
}