javascript - 用户离开页面之前的 JavaScript 确认,而不是页面加载
问题描述
我希望用户仅在离开页面以确认他们想要时看到警报。目前,我认为任何正在发生的事情也都在Page_Load
显示警报。
我有这个代码:
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Changes you made may not be saved.";
}
预期:仅当退出或单击返回时显示确认警报。
实际:DropDownList
我有导致警报显示以及离开页面。
编辑:
<asp:DropDownList ID="ddlJob" runat="server" AutoPostBack="true" CssClass="combobox"
DataTextField="name" DataValueField="name" OnClick="hideOnKeyPress(); return true;">
</asp:DropDownList>
解决方案
尝试这个:
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = 'Changes you made may not be saved.';
});
推荐阅读
- c# - 使用 thead 标签时如何防止 PuppeteerSharp(HTML 到 PDF)在新页面中开始表格行
- r - 对于每个带有正则表达式的循环
- php - 在 PHP 中删除 cookie 中的会话 ID
- unity3d - 如何将 Input.GetAxis("Mouse X") 或 Input.GetAxis("Mouse Y") 转换为新的输入系统?
- c++ - C++ undefine 宏未按预期工作
- python - 设计建议从 Airflow 调用 Cloud Build 以执行带有 args 的 python 函数
- r - 如何在R中按日期范围拆分时间序列数据?
- python - 如何使用 Python 中的请求从 Reddit 页面的帖子中获取所有图像链接
- javascript - Javascript / 如果,否则
- r - 包 obdc 列在 CRAN-R 上,但无法安装