Is there any way to detect if user pressed “Stay on page” or “Leave page” in beforeunload event?

牧云@^-^@ 提交于 2019-11-30 21:39:09

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

iirc, the unload event is not triggered, when the user decides to stay on the page. so:

$(window).on('unload', function (){
    // user has left the page, do stuff here
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!