On browser close beforeunload - how to find if user selected “Stay” or “Leave”

谁都会走 提交于 2019-12-24 14:54:16

问题


To keep things very simple - I have the following code

$(window).bind("beforeunload",function(event) {
    return '';
})

Atleast on Chrome,FireFox and IE9 - when a user closes the browser - he is presented with two options - "Stay on Page" or "Leave this Page" (The message varies from browser to browser)

Now how can I find out what option the user selected For sake of simplicity - lets say - I want to show an extra message based on which option the user selected

Something like - just to give an idea

if(option=="Stay on Page")
   alert("Thanks for continuing to stay");
if(option=="Leave this page")
   alert("Sorry you decided to go - please come back soon");

How do I capture - what the user selected. Thanks much


回答1:


As far as I know, you are not allowed to do that, for security reasons. The only thing you can do is the below:

window.onbeforeunload = function(){
    return 'Your confirmation message';
}


来源:https://stackoverflow.com/questions/11271919/on-browser-close-beforeunload-how-to-find-if-user-selected-stay-or-leave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!