简体   繁体   English

退出时的Java确认框

[英]Javascript Confirmation Box on Exit

I got strange problem with setting up a confirmation box on user exit. 我在用户出口设置确认框时遇到了奇怪的问题。

Here's the code: 这是代码:

    function closeIt() {
    var message = "Click 'Cancel' right now!";

    evt=(typeof evt == 'undefined') ? window.event:evt;
    evt ? evt.returnValue = message:null;
    return message;
    }   
    window.onbeforeunload = closeIt;

What functionality I'd like to implement? 我想实现什么功能? When user clicks Cancel, I'd like to redirect page. 当用户单击“取消”时,我想重定向页面。

I trie milions of ideas, non of them was working. 我想出了数百万个想法,但没有一个在起作用。 Any ideas? 有任何想法吗?

Edit This is what I'd like to have: Users tries to close his browser tab, I'd like to display small confirmation box, if he clicks OK -> then close the tab, if he clicks Cancel -> do not close the tab and redirect user to save page. 编辑这是我想要的内容:用户尝试关闭其浏览器选项卡,我想显示一个小的确认框,如果他单击“确定”->然后关闭该选项卡,如果他单击“取消”->不要关闭该选项卡标签,然后将用户重定向到保存页面。

I tried milions of methods, faced hunderds of problems. 我尝试了数百万种方法,但遇到了很多问题。

So... how to get user action then? 那么...然后如何使用户采取行动呢?

You actually can't, directly. 您实际上不能直接这样做。 The browser takes charge of showing the confirmation, and either continuing the navigation if OK, or doing nothing if Cancel. 浏览器负责显示确认,如果确定,则继续导航,如果取消,则不执行任何操作。 You don't get to find out whether the user clicked OK until you get the final unload , by which point it's too late. 在获得最终unload之前,您无法确定用户是否单击“确定”,到那时为时已晚。

One strategy is to guess that, if the user is still around on the page a short while after the beforeunload , they probably clicked Cancel: 一种策略是猜测,如果用户在beforeunload之后beforeunload仍停留在页面上,则他们可能单击了Cancel:

var redirecting= false;
window.onbeforeunload = function() {
    if (redirecting) return;
    setTimeout(function() {
        setTimeout(function() {
            redirecting= true;
            location.href= 'http://www.example.com/';
        }, 2000);
    }, 0);
    return 'Boo hoo, please stay with me';
};

(The redirecting flag is to stop our own navigation also causing a beforeunload warning; the double-timeout is to ensure that the timeout occurs one second after the confirmation box is closed, rather than a second after it has opened.) redirecting标志是要停止我们自己的导航,这还会引起beforeunload警告;双重超时是为了确保超时在确认框关闭后一秒钟发生,而不是在确认框打开后发生一秒钟。)

Unfortunately this is still highly unreliable, as it will perform the redirect even if the user clicked OK, but the page to which they're navigating is taking more than two seconds to start loading. 不幸的是,这仍然非常不可靠,因为即使用户单击“确定”,它仍会执行重定向,但是他们要导航到的页面要花费两秒钟以上的时间才能开始加载。

Another way would be to always return nothing from beforeunload prompt, and then set up your own explicit confirm() on a 0-timeout. 另一种方法是始终不从beforeunload提示符返​​回任何beforeunload ,然后在0超时时设置自己的显式beforeunload confirm() But this is unreliable cross-browser, as understandably browsers don't really want to let pages do potentially obnoxious stuff like this. 但这是不可靠的跨浏览器,因为可以理解的是,浏览器并不想让页面做这样的潜在令人讨厌的事情。 It kind of works on IE and almost works on Firefox, as long as you break the back/forward cache (as this hides the page before the confirm can happen). 只要您中断后退/前进缓存(因为这会在确认发生之前将页面隐藏),它就可以在IE上运行,并且几乎可以在Firefox上运行。

In summary there's not really a good solution. 总之,这并不是一个很好的解决方案。 What's the use case for redirecting the user? 重定向用户的用例是什么? Perhaps you could try replacing the content of the current page rather than doing a redirect? 也许您可以尝试替换当前页面的内容而不是进行重定向?

is this what you want? 这是你想要的吗?

<script type="text/javascript">
window.onbeforeunload = function (evt) {
var message = 'Are you sure you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
window.location = "http://www.google.com/";
}
return message;
}
</script>

you can simply replace google.com with your url. 您只需将google.com替换为您的网址即可。 i used google.com for testing :) 我用google.com进行测试:)
Cheers ! 干杯!

Works for me usign confirm . 对我工作的confirm I sincerely didn't understand how you tried to solve it, given no confirm is issued. 由于没有发出confirm ,我真诚地不理解您如何尝试解决该问题。

window.onbeforeunload = function(){
  return confirm("Click 'Cancel' right now!");
}

I'm working with the same type of problem (with Dojo v1.3) (see http://jsfiddle.net/ZPxtj/19/ ). 我正在处理相同类型的问题(使用Dojo v1.3)(请参阅http://jsfiddle.net/ZPxtj/19/ )。

It looks like: 看起来像:


window.onbeforeunload = function() {
    ...
};

Is working well. 运作良好。

Have you tried starting a timer event in the onbeforeUnload event handler? 您是否尝试过在onbeforeUnload事件处理程序中启动计时器事件? They usually don't fire as long as there is a modal dialog on the screen like the confirm. 只要屏幕上出现诸如确认之类的模态对话框,它们通常就不会触发。 (This could be platform dependent!) (这可能取决于平台!)

The timer may get killed automatically when the user presses OK. 当用户按“确定”时,计时器可能会自动终止。 If not, you need to stop it in onunload. 如果不是,则需要在onunload中停止它。

If your timer does go off, you redirect the page in the timer event handler. 如果您的计时器确实关闭,则可以在计时器事件处理程序中重定向页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM