JavaScript实现简易用户登录及自动跳转功能

作者:纸飞机时间:2026-10-27 09:37:36浏览:103

本文介绍如何使用JavaScript实现简易的用户登录验证及登录成功后的页面自动跳转功能。通过HTML表单收集用户名和密码,利用JS判断输入是否正确。若验证通过,页面将跳转至成功页,并配合setInterval实现倒计时显示,随后使用setTimeout在指定时间后自动跳转至最终页面。

登录验证与页面跳转逻辑

当用户输入密码为123时,系统跳转至第二个页面,并在4秒后自动跳转至第三个页面。对应的登录页面文件名为login.html。

具体实现代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript">
//进行检查
function check(){
//window.alert($("username").value);
if($("username").value=="pwd" && $("pwd").value=="123"){
return true;
}else{
$("username").value="";
$("pwd").value="";
return false;
return true;
}else{
$("username").value="";
$("pwd").value="";
return false;
        }
    }
//获取文档对象
function $(id){
return document.getElementById(id);
    }
</script>
</head>
<body>
<form action="ok.html">
u:<input type="text" id="username" /><br/>
p:<input type="password" id="pwd" /><br/>
<input type="submit" value="提交" onclick="return check()"/>
</form>
</body>
</html>

ok.html

第二个页面(ok.html)的代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript">
//跳转到指定页面
function tiao(){
clearInterval(myInterval);
window.open("success.html","_self");
clearInterval(myInterval);
window.open("success.html","_self");
    }
//设置5秒跳转
setTimeout("tiao()",5000);
//改变秒数
function changeSec(){
//改变数字
$("myspan").innerText=parseInt($("myspan").innerText)-1;
    }
var myInterval=setInterval("changeSec()",1000);
//获取对象
 function $(id){
 return document.getElementById(id);
 }
</script>
</head>
<body>
登录成功<span id="myspan">5</span>秒后自动跳转
</body>
</html>
//获取对象
 function $(id){
 return document.getElementById(id);
 }
</script>
</head>
<body>
登录成功<span id="myspan">5</span>秒后自动跳转
</body>
</html>

success.html

第三个页面(success.html)的代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript">
</script>
</head>
<body>
欢迎.......
</body>
</html>

作者最新文章