<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" name="" id="inputID" />
<script>
let isChange = false;
const inputEl = document.getElementById("inputID");
inputEl.addEventListener("change", function(ev) {
console.log(ev.target.value);
isChange = true;
});
window.onbeforeunload = function(e) {
return !isChange && null; // onbeforeunload 钩子中如果返回null的话,就不会弹出对话框("系统可能不会保存您所做的更改")。
// 暂时没找到可以改变弹出框文案的方法,应该是不可以的。
// 以下代码没有效果
// var dialogText = "Dialog text here";
// e.returnValue = dialogText;
// return dialogText;
};
</script>
</body>
</html>
onbeforeunload 处理“系统可能不会保存您所做的更改“弹框
最新推荐文章于 2024-03-15 10:35:09 发布