文章详情

返回首页

在html页面添加输入密码才能打开

分享文章 作者: Ws01 创建时间: 2025-11-26 📝 字数: 1,623 字 👁️ 阅读: 12 次
1、密码24小时有效【 使用时请修改 admin 为自己的密码 】 <script> // 页面加载时弹出密码验证 (function() { // 检查是否已存储有效密码 const storedAuth = localStorage.getItem('imagePageAuth'); if (storedAuth) { const authData = JSON.parse(storedAuth); const now = new Date().getTime(); // 检查密码是否在24小时内(24*60*60*1000 = 86400000毫秒) if (now - authData.timestamp < 86400000) { // 密码仍在有效期内,允许访问 return; } else { // 密码已过期,清除存储 localStorage.removeItem('imagePageAuth'); } } // 如果没有有效密码,则要求输入 var password = prompt("请输入密码访问本页面:"); if (password !== "admin") { alert("密码错误!禁止访问。"); window.location.href = "about:blank"; // 密码错误跳转至空白页 } else { // 密码正确,存储当前时间和密码信息(24小时内有效) const authData = { password: password, timestamp: new Date().getTime() }; localStorage.setItem('imagePageAuth', JSON.stringify(authData)); } })(); </script> 2、密码当次有效,下一次打开也需要输入【 使用时请修改 admin 为自己的密码 】 <script> // 页面加载时弹出密码验证 (function() { var password = prompt("请输入密码访问本页面:"); if (password !== "admin") { alert("密码错误!禁止访问。"); window.location.href = "about:blank"; // 密码错误跳转至空白页 } })(); </script>