javascript 当我处于 onbeforeunload 事件时如何防止浏览器选项卡/窗口关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33659948/
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
How to prevent browser tab/window close when I'm in onbeforeunload event?
提问by Dumbledore
I want to show a custom Are you sure you want to leave this page?dialog box (in ExtJs) in the window.onbeforeunload
event. I am familiar with the fact that if I return a string from this method, it would show a "browser" pop up with the returned message on it; however, I don't want the regular pop up at all.
我想显示自定义您确定要离开此页面吗?事件中的对话框(在 ExtJs 中)window.onbeforeunload
。我熟悉这样一个事实:如果我从这个方法返回一个字符串,它会显示一个“浏览器”弹出窗口,上面有返回的消息;但是,我根本不希望定期弹出。
Now, the issue is that as soon as window.onbeforeunload
function finishes executing, ExtJs dialog box or anything on the screen doesn't matter anymore and the browser tab closes. I tried stopImmediatePropagation
, stopPropagation
methods and setting cancelBubble
property to true
. However, nothing seems to work. I'm sorry, I don't even know if they are supposed to work.
现在,问题是一旦window.onbeforeunload
函数完成执行,ExtJs 对话框或屏幕上的任何内容都不再重要,浏览器选项卡将关闭。我试过stopImmediatePropagation
,stopPropagation
方法和设置cancelBubble
属性true
。但是,似乎没有任何效果。对不起,我什至不知道他们是否应该工作。
Here is the code:
这是代码:
window.onbeforeunload = function(e){
Ext.Msg.show({
title:'Warning',
msg: 'Are you sure?',
buttons: Ext.Msg.YESNO,
icon: Ext.Msg.QUESTION,
fn: function (btn) {
if (btn == 'yes') {
//Close Browser Window
}
},
modal: true
});
//A desperate attempt to stop window/tab from closing
e.stopPropagation();
e.cancelBubble = true;
e.stopImmediatePropagation();
//This brings up default browser pop up
//return 'Message';
};
I'm sorry, I won't be able to create a Sencha Fiddle because it has it's own Confirm Navigationdialog box.
抱歉,我无法创建 Sencha Fiddle,因为它有自己的“确认导航”对话框。
I want to know if it's possible to implement this? If yes, then how?
我想知道是否可以实现这个?如果是,那么如何?
Thank you so much.
太感谢了。
采纳答案by Danil Gudz
Here is an explanationabout this no choice to make a custom (extjs etc) dialog
下面是一个解释这个没有选择创建一个自定义(ExtJS的等)对话