javascript - 手动刷新页面时如何取消浏览器关闭/刷新确认模式?
问题描述
当用户使用以下代码单击浏览器的刷新或关闭按钮时,我让浏览器显示确认模式:
window.onbeforeunload = confirmCloseOrRefresh;
function confirmCloseOrRefresh() {
var confirmMessage = confirm('Are you sure to exit?');
return confirmMessage;
}
现在,我的页面上有一个自定义刷新按钮,我想手动刷新页面而不显示确认模式。
我试过这个:
onRefreshButtonClick(event) {
event.preventDefault();
window.location.reload();
return true;
}
仍然显示确认模式。有什么方法可以取消/覆盖此功能中的确认模式?
解决方案
您可以简单地引入您在函数restrictUnload
中检查的全局布尔变量:confirmCloseOrRefresh
var restrictUnload = true;
function confirmCloseOrRefresh() {
if (restrictUnload) {
var confirmMessage = confirm('Are you sure to exit?');
return confirmMessage;
} else {
return true;
}
}
在您的onRefreshButtonClick
函数中,只需将该变量设置为false
:
onRefreshButtonClick(event) {
restrictUnload = false;
event.preventDefault();
window.location.reload();
return true;
}
推荐阅读
- python - 用于过滤仅具有名称和标签值的 describe_images 函数的 Aws lambda 函数
- c - C 是否有 HTTP2 的标准库
- cdn - 重新验证缓存更新填充到所有 POP 时 CDN 是否过时?
- amazon-web-services - 在 AWS 上的何处存储多个访问令牌?
- docker - 我们如何将两个工具划分为通过标准输入标准输出进行对话的微服务
- javascript - HTML模板内容和innerHTML的区别
- parse-platform - 是否可以仅对部分用户启用电子邮件验证?
- ios - 参数“-fuse-ld=lld”中的链接器名称无效?
- api - Dart/Flutter 如何在 Dart 应用程序中使用来自 woocommerce API 的参数?
- mysql - MySql Workbench 错误 1064,我该如何解决这个错误?