gpt4 book ai didi

javascript - 将 HTML 页面指向登录页面

转载 作者:行者123 更新时间:2023-11-30 08:38:29 25 4
gpt4 key购买 nike

我创建了两个 HTML 页面,一个名为 login.html,另一个名为 resume.html

我想做的是,无论我打开哪个页面,我都会被重定向到登录页面。如果我打开 login.html,它将被打开并填写 usernamepassword 字段,然后 resume .html 打开。

我的问题是,当我打开 resume.html 时,我不知道应该添加的代码会被重定向到 login.html .

登录页面的代码是:

  <html>
<head>

<title>
Login page
</title>
</head>

<body>
<h1 style="font-family:Comic Sans Ms;text-align:center;font-size:50pt;color:#ff00ba;">
You need to login :) </h1>

<form style="font-family:Comic Sans Ms; text-align:center">
Username<br>
<input type="text" name="userid"/><br>
Password<br>
<input type="password" name="pswrd"/>
<br><br>
<input style="font-family:Comic Sans Ms;" type="button" onclick="check(form)" value="Login">
<input style="font-family:Comic Sans Ms;" type="reset" value="Cancel"/>
</form>


<script language="javascript">
function check(form)
{

if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
window.open('resume.html')
}
else
{
alert("Error Password or Username")
}
}
</script>
</body>
</html>

最佳答案

您可以在localStorage中注册用户是否登录,点击按钮时。

所以改变这个:

if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
window.open('resume.html')
}

为此:

if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
localStorage.setItem('isLoggedIn', true);location.href='/resume.html';
}

login.html 中将注销按钮添加到您的 html ,以便能够注销,当然:

<input style="font-family:Comic Sans Ms;" type="button" value="Log out" onclick="localStorage.removeItem('isLoggedIn');location.href='/login.html'"/>

并在 <head> 中添加此代码你的/resume.html以及您希望仅在登录后才能访问的任何其他页面:

<script>
window.onload=function(){
if(localStorage.getItem('isLoggedIn')==null){
location.href="/login.html";
}
}
</script>

关于javascript - 将 HTML 页面指向登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29174301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com