window.location.href is not redirecting the user.
I have tried with return true also
Form Code:-
<form method="POST" name="main_form" onsubmit="login()">
<input type="email" name="email">
<input type="password" name="pwd">
<input type="submit">
</form>
Login():-
function login() {
var email = document.main_form.email.value;
var pwd = document.main_form.pwd.value;
if (email == 'example@example.com' && pwd == 'example123') {
sessionStorage.setItem("email", email);
window.location.href = './index.html';
return false;
Apart from not storing userid and password in the page, you need to use
onsubmit="return login()"
and
function login() {
var email = document.main_form.email.value;
var pwd = document.main_form.pwd.value;
if (email == 'example@example.com' && pwd == 'example123') {
sessionStorage.setItem("email", email);
window.location.href = './index.html';
}
return false;
}
Better is using event listeners:
document.querySelector("form[name=main_form]").addEventListener("submit",function(e) {
e.preventDefault(); // stop submission
const email = this.email.value; // "this" is the form due to using function in the eventListener. Use a fat arrow and you need e.target
const pwd = this.pwd.value;
if (email == 'example@example.com' && pwd == 'example123') {
sessionStorage.setItem("email", email);
window.location.href = './index.html';
}
});