Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!