Html 退出对话框前确认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17143394/
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
confirmation before exit dialog
提问by slice
In most pages, if your doing an action, (like editing, creating), and when I attempt to exit, it mostly prompts me if I really want to exit. The dialog gives me two options: leave or cancel and continue. How do you accomplish this in JavaScript? Do I have to use a meta element? And please don't mention beforeunload
, unless it's the true and only way to accomplish this.
在大多数页面中,如果您执行某个操作(例如编辑、创建),并且当我尝试退出时,它通常会提示我是否真的要退出。该对话框给了我两个选项:离开或取消并继续。你如何在 JavaScript 中实现这一点?我必须使用元元素吗?并且请不要提及beforeunload
,除非这是实现这一目标的真实且唯一的方法。
回答by casraf
Why not mention onbeforeunload
? It's the built in way to do so, and I don't see a problem using it.
为什么不提onbeforeunload
?这是这样做的内置方式,我认为使用它没有问题。
function myConfirmation() {
return 'Are you sure you want to quit?';
}
window.onbeforeunload = myConfirmation;
回答by Eos Antigen
what I use is:
我使用的是:
onClick="return confirm('Sure you want to exit..?')"
回答by Alex Ruhl
window.onbeforeunload = function() {
if(confirm('are you sure to exit?'))
return true;
else
return false;
};