在成功登录时,我试图将用户重定向到另一个网站。下面的代码不起作用,无法解释原因。
我用过; 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>
答案 0 :(得分:2)
您的按钮正在尝试汇总表单。这可能是一个奇怪的解决方案,但将按钮type
属性更改为除提交之外的任何内容。
例如:
<button type="button" onclick="myFunction()">Login</button>
答案 1 :(得分:0)
首先,您应该在jquery document.ready或匿名函数中执行整个操作。其次,在loginCheck定义中,使用回调来调用结果检查
<button id="buttonId">Login</button>
然后在js
;(function(){
document.getElementById('buttonId').addEventListener('click',function(){
var uName= document.getElementById("username").value;
//alert(uName);
var pwd= document.getElementById("password").value;
//alert(pwd);
loginCheck(uName, pwd, function(){
//if true
if(result==true){
//re-direct to stack overflow
window.location.href= "http://stackoverflow.com";
}
else{
alert("you have failed");
}
});
});
})();
在函数参数中添加回调并在完成检查时调用
答案 2 :(得分:0)
重定向使用window.location
private Process(SerializationInfo info, StreamingContext context)
{
CycleTime = info.GetDouble("CycleTime");
CycleTimeUnit = (TimeUnit) info.GetValue("CycleTimeUnit", typeof(TimeUnit));
}