使用JavaScript在离开页面前通过onbeforeunload判断并提示用户
作者:闭包旅人时间:2026-10-28 09:55:37浏览:653
本文介绍如何在HTML页面中使用JavaScript的onbeforeunload事件,在用户尝试关闭或离开当前页面前进行条件判断。通过获取特定输入框的值,若为空则弹出确认对话框,防止用户误操作离开页面。
</html>
具体实现代码如下所示:
JavaScript离开页面提示实现代码<!doctype html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>js离开当前页面前判断条件提示是否要离开页面</title>
<script type="text/javascript">
window.onbeforeunload = function() {
{
var unloads = document.getElementById("unloads").value;
if(unloads == null || unloads == "") {
return "您确定要退出页面吗?";
}
}
}
</script>
</head>
<body>
<input type="text" id="unloads" name="unloads" value="" />
</body>作者最新文章
什么时候该用JS构造器:与工厂模式、字面量的选择对比
2026-11-05 16:18:22
推荐几款轻量级JS照片墙开源库及对比
2026-11-05 15:56:19
JavaScript递归写法有哪些常见陷阱及避免方法
2026-11-05 14:53:38
JS数组浅拷贝与深拷贝怎么选?场景与方法总结
2026-11-05 14:41:02
p5.js 中 setup() 和 draw() 函数的执行机制详解
2026-11-05 12:32:38
