Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Why is my Javascript redirect to website not working?

i made a simple login website that uses javascript to run a function that checks username and password:

the website:

   <!DOCTYPE html>
   <html>
   <head>
    <meta charset="utf-8">
    <title>Mosswood Herb</title>
    <link rel="stylesheet" href="loginPage.css">
    <script src="loginPage.js"></script>
    </head>
    <body>

    <form class="box" action="index.html" method="post">
    <h1>Login</h1>
    <input type="text" id="username" name="username" placeholder="username">
    <input type="password" id="password" name="password" placeholder="password">
    <input type="submit" name="" value="submit" onclick=submitBtn()>
    </form>

     </body>
     </html>

and the javascript:

let user, pass;

function submitBtn() {
user = document.getElementById("username").value;
pass = document.getElementById("password").value;

if (user == 'oleg' && pass == 'oleg') {
    window.location.replace('https://www.youtube.com/')
} else {
    alert('Incorrect Username or Password');
}
}


 document.addEventListener('keypress', logKey);

   function logKey(e) {
    if (e.key == 'Enter') {
     submitBtn();
    } else {}
  }

the website functions normally, and will display an error message saying 'invalid' when the username and pass is not reached. however, when the user and pass is correct, it will not redirect me to youtube.com? not too sure why it's happening

P.S the indentation is correct, only had to change it because stack overflow are tedious ;)

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda