Javascript 密码验证窗口.location.replace.

Javascript Password Verification window.location.replace

本文关键字:location replace 窗口 验证 密码 Javascript      更新时间:2023-09-26

我有一个网站,我想制作一个JavaScript登录表单。

不像一个完美的用户名和密码登录,但我想建立一个密码验证登录。我希望用户在正确时重定向到私有.html当他们做错时.html重定向到错误。

这是 HTML:

<script type="text/javascript">
function checkForm() {
  x = document.getElementById("holy").value
  if (x == "mangos") {
    alert("Right Password!");
    window.location.replace("private.html");
  }
  else {
    alert("Wrong Password!");
    window.location.replace("home.html");
  }
}
</script>
<center>
<form class="form-container">
<center>
<div class="form-title"><h2>Login</h2></div>
<div class="form-title">Username</div>
<input class="form-field" type="text" name="username" /><br />
<div class="form-title">Password</div>
<input class="form-field" type="password" id="holy" name="password" /><br />
<div class="submit-container">
<button onclick="checkForm()" class="submit-button">Login</button>
</div> 
</center>
</form>

显然,函数中的警报部分有效,但是代码的下一部分,即 window.location.replace 根本不起作用。相反,网址变为

.com/password.html?username=asd&password=mangos

我应该怎么做/解决这个问题?

发生这种情况是因为表单正在提交,这是它的默认行为。要避免这种情况,请在 checkForm() 函数的末尾编写一个返回 false。

function checkForm() {
  x = document.getElementById("holy").value
  if (x == "mangos") {
    alert("Right Password!");
    window.location.replace("private.html");
    return false;
  }
  else {
    alert("Wrong Password!");
    window.location.replace("home.html");
    return false;
  }
}