且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有什么方法可以检测用户是否按下了“留在页面上"?或“离开页面"在 beforeunload 事件中?

更新时间:2023-12-01 21:28:28

通过一些技巧,您至少可以确定用户是否留下.
如果用户离开了页面,那么您无能为力:

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?";
});

FIDDLE