javascript 成功登录后如何使用javascript重定向到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19624724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect to another page using javascript after successful login?
提问by Dede
I need to have a login form that validates and after login the page needs to redirect to a html page. I also need to hard code in a username and password so only with the correct user name/password the page will redirect. If it doesn't have the correct user/pass then I need an alert.
我需要一个登录表单来验证,登录后页面需要重定向到 html 页面。我还需要对用户名和密码进行硬编码,因此只有使用正确的用户名/密码才能重定向页面。如果它没有正确的用户/通行证,那么我需要一个警报。
I've tried a bunch of variations but I know I missing something or using the code wrong. Please help! :) Thanks! This is my code so far.
我尝试了很多变体,但我知道我遗漏了一些东西或使用了错误的代码。请帮忙!:) 谢谢!到目前为止,这是我的代码。
JS:
JS:
function validateForm() {
'use strict';
// Get references to the form elements:
var email = document.getElementById('email');
var password = document.getElementById('password');
// Validate the login
if ((email.value.length > 0) && (password.value.length > 0)) {
return true;
} else {
alert('Please complete the form!');
return false;
}
}
function check(form) {
var emailArray = ("[email protected]", "");
var passwordArray = ("MyLogIn", "");
if (email.value == "email" && password.value == "password") {
window.open('myaccount.html');
} else {
alert('Please enter correct username or password!');
return false;
}
}
function init() {
'use strict';
// Confirm that document.getElementById() can be used:
if (document && document.getElementById) {
var loginForm = document.getElementById('lgform');
loginForm.onsubmit = validateForm;
}
}
window.onload = init;
HTML:
HTML:
<form action="" method="post" id="lgform">
<fieldset>
<legend>Login</legend>
<div>
<label for="email">Email Address</label>
<input type="email" name="email" id="email" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
</div>
<div>
<label for="submit"></label>
<input type="submit" value="Login" id="submit">
</div>
</fieldset>
</form>
回答by m.edmondson
Hardcoding the username and password client side is very dangerous - don't do it.
硬编码客户端的用户名和密码是非常危险的 -不要这样做。
In order to redirect you simply:
为了简单地重定向您:
window.location = "http://www.google.com/"
window.location = "http://www.google.com/"
however I very much doubt the page you are directing too is secure, can it be accessed directly via the url without passing your authentication?
但是我非常怀疑您指向的页面是否安全,是否可以通过 url 直接访问而无需通过您的身份验证?