成功登录后,我尝试将用户重定向到另一个网站。下面的代码不起作用,也不知道为什么。
我用过; window.location.href、document.location.href 和 window.location.replace 也是如此。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="login.css" type="text/css">
<script src="login.js">
</script>
</head>
<body>
<div class="login-page">
<div class="form">
<form class="register-form">
<input type="text" placeholder="name"/>
<input type="password" placeholder="password"/>
<input type="text" placeholder="email address"/>
<button>create</button>
<p class="message">Already registered? <a href="#">Sign In</a></p>
</form>
<form class="login-form">
<input type="text" placeholder="username" id="username" value="" required/>
<input type="password" placeholder="password" id="password" value="" required/>
<button onclick="myFunction()">Login</button>
<p class="message">Not registered? <a href="#">Create an account</a></p>
<script>
function myFunction(){
var uName= document.getElementById("username").value;
//alert(uName);
var pwd= document.getElementById("password").value;
//alert(pwd);
var result = loginCheck(uName, pwd);//loginCheck function external
//if true
if(result==true){
//re-direct to stack overflow
window.location.href= "http://stackoverflow.com";
}
else{
alert("you have failed");
}
}
</script>
最佳答案
您的按钮正在尝试提交表单。这可能是一个奇怪的解决方案,但将按钮 type
属性更改为除 submit 之外的任何内容。
例如:
<button type="button" onclick="myFunction()">Login</button>
关于javascript - 在 JavaScript 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697848/