Javascript 是否有取消 window.onbeforeunload 的回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11835217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:18:11  来源:igfitidea点击:

Is there a callback for cancelling window.onbeforeunload

javascript

提问by Robin Drexler

I don't have an actual use case for this, but I'm curious, whether there is a way to react (callback), if a user clicks on "stay on this page" when window.onbeforeunload was triggered.

我没有这方面的实际用例,但我很好奇,如果用户在 window.onbeforeunload 被触发时点击“停留在此页面”,是否有办法做出反应(回调)。

http://jsfiddle.net/rWHU9/

http://jsfiddle.net/rWHU9/

function warning(){
    if(true){
      console.log('leaving');
      return "You are leaving the page";
    }
}
window.onbeforeunload = warning;?

采纳答案by Rocket Hazmat

There is no callback for staying on the page, but there is one for leaving the page, window.unload.

停留在页面没有回调,但是有一个离开页面,window.unload

Try setting a timeout in beforeunload, then clear it in unload. If you stay, the timeout will run, otherwise it'll be cleared.

尝试在 中设置超时beforeunload,然后在卸载中清除它。如果你留下,超时将运行,否则将被清除。

var timeout;

function warning() {
    timeout = setTimeout(function() {
        alert('You stayed');
    }, 1000);
    return "You are leaving the page";
}

function noTimeout() {
    clearTimeout(timeout);
}

window.onbeforeunload = warning;
window.unload = noTimeout;?

DEMO: http://jsfiddle.net/aPwfz/1/

演示:http: //jsfiddle.net/aPwfz/1/

回答by jAndy

Short answer: No, there is no callback or any directway to intercept the "stay on this page" event, yet.

简短回答:不,目前还没有回调或任何直接方法来拦截“停留在此页面”事件。

However, you can cheat a little and create a construct like this:

但是,您可以稍微作弊并创建这样的构造:

function warning() {
    setTimeout(function() {
        setTimeout(function() {
            alert('user choosed to stay on this site: ' + location.href);
        }, 100);
    },1);
    return "You are leaving the page";
}
window.onbeforeunload = warning;?

Since the question is a modal dialog, the setTimeoutcallback will not fire until you confirmed that modal box. But as you correctly assume now, this would also trigger if you click "leave this site". So this is tricky. To solve that, you would need to increase the setTimeoutto give the browser enough time to unloadthe page.

由于问题是模态对话框,因此setTimeout在您确认该模态框之前不会触发回调。但是正如您现在正确假设的那样,如果您单击“离开此站点”,这也会触发。所以这很棘手。要解决这个问题,您需要增加setTimeout以使浏览器有足够的时间访问unload该页面。

Why there are two setTimeout()'s nested ? Well, the timer will "run" as soon as the modal dialog pops up. So we don't want to run that timer until the modal dialog was closed. As soon as that happens, our inner setTimeoutwill launch and do something over time.

为什么有两个setTimeout()嵌套?好吧,一旦弹出模态对话框,计时器就会“运行”。所以我们不想在模式对话框关闭之前运行那个计时器。一旦发生这种情况,我们的内在setTimeout就会随着时间的推移而启动并做一些事情。

I think a realistiv value for most real-world scenarios is about 2.000ms for the inner setTimeout

我认为大多数现实世界场景的现实价值是关于2.000内部的 mssetTimeout

回答by jbabey

The only way to tell that the user clicked cancel is to trigger a setTimeoutand see if it fires. There is no way to interrogate the return from the system's confirm-esque box.

告诉用户单击取消的唯一方法是触发 asetTimeout并查看它是否触发。没有办法查询系统的confirm-esque 框的返回值。

http://jsfiddle.net/rWHU9/2/

http://jsfiddle.net/rWHU9/2/

回答by Rudolfo Pacheco

Try triggering a setTimeout as the first code to call any other function (in say 5-10 seconds) BEFORE the return "question here". i.e.

在返回“此处的问题”之前,尝试触发 setTimeout 作为调用任何其他函数的第一个代码(例如 5-10 秒)。IE

window.onbeforeunload = confirmExit;
function confirmExit() {
    setTimeout("thisFunction;", 10000);
    return "do you realy want to close?";
}

And to cancel the onbeforeclose this worked for me: try an empty function:

并取消 onbeforeclose 这对我有用:尝试一个空函数:

window.onbeforeunload = function(){};

Thanks, good luck!

谢谢,祝你好运!

回答by vickisys

$(window).bind('beforeunload', function(){
     return '\n Your custom message';
 });

With ‘beforeunload‘ event attached, when you close the page, hits the back button or reload the page, a confirmation box will prompt to ask you whether you really want to go, choose ok to exit the page: cancel to stop the page from exit and stay on the same page.

附加了'beforeunload'事件,当你关闭页面、点击后退按钮或重新加载页面时,一个确认框会提示你是否真的要去,选择确定退出页面:取消停止页面退出并停留在同一页面上。