[英]How to cancel browser close/refresh confirm modal when refreshing page manually?
I have the browser show confirm modal when the user clicks the browser's refresh or close buttons with the code below: 当用户使用以下代码单击浏览器的刷新或关闭按钮时,浏览器显示确认模式:
window.onbeforeunload = confirmCloseOrRefresh;
function confirmCloseOrRefresh() {
var confirmMessage = confirm('Are you sure to exit?');
return confirmMessage;
}
Now, I have a custom refresh button on my page which I want to manually refresh the page without showing the confirm modal. 现在,我在页面上有一个自定义刷新按钮,我想手动刷新页面而不显示确认模式。
I tried this: 我尝试了这个:
onRefreshButtonClick(event) {
event.preventDefault();
window.location.reload();
return true;
}
Still shows the confirm modal. 仍显示确认模式。 Any way to cancel/override the confirm modal in this function? 有什么方法可以取消/覆盖此功能中的确认模式?
You could simply introduce a global Boolean variable restrictUnload
which you check in your confirmCloseOrRefresh
function: 你可以简单地引入全球布尔变量restrictUnload
,你在你的检查confirmCloseOrRefresh
功能:
var restrictUnload = true;
function confirmCloseOrRefresh() {
if (restrictUnload) {
var confirmMessage = confirm('Are you sure to exit?');
return confirmMessage;
} else {
return true;
}
}
In your onRefreshButtonClick
function, simply set that variable to false
: 在您的onRefreshButtonClick
函数中,只需将该变量设置为false
:
onRefreshButtonClick(event) {
restrictUnload = false;
event.preventDefault();
window.location.reload();
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.