Hi I’m html learner and I have an issue with my code but I don’t know what’s the problem
The prob is weird , when i enter the right pass in only have my alert « Le mot de passe est correct » but the page is just reload and not move to https://mycool-url.com
This is a simple html code it’s for learn password
<title>Authentification</title>
</head>
<body>
<div >
<p align="center">Veuillez vous identifier</p>
<form >
<input type="password" align="center" id="password" placeholder="Mot de Passe" type="password" name="pswrd">
<input align="center" type="submit" onclick="checkPassword()" value="Login" />
</form>
</div>
<script>
function checkPassword() {
if (document.getElementById('password').value == 'ZhFAIHhyRvBaZ') {
alert('Mot de passe correct')
location.replace('https://www.mycool-url.com');
} else {
alert('Mot de passe incorrect');
return false;
}
}
</script>
</script>
</body>
CodePudding user response:
The form is submitting. If you don't want to submit a form, change the button type from type="submit"
to type="button"
:
<input align="center" type="button" onclick="checkPassword()" value="Login" />
If you're not using a "form" at all, you can even just remove the <form>
element entirely, which would prevent accidental form submission.
As an aside, it's also worth noting that any user can see the "password" or go directly to the target URL so this authentication mechanism doesn't secure anything.
CodePudding user response:
Replace location.replace('https://www.mycool-url.com');
by window.location.replace("https://www.mycool-url.com");