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

154
Views
IF condition not working properly in javascript - localStorage

I'm trying to put together a VERY simple login page to test some things out (I'm not a web dev) and I created a script in javascript that sets an authorized variable to TRUE once the user successfully logs in and redirects the user to the index page. Once there, the body onload runs a function that checks to see if the authorized variable is true to allow the user to stay in the index page and than sets it to false.

The thing is that after I refresh the page the variable is now false (which it should be) but the FALSE condition is not running. It keeps running only the true condition.

Any thoughts?

    function validate()
{
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    if(username=="" && password=="")
    {
        alert("Please fill all the fields.")
        return true;
    }
    else if (username=="adm" && password=="test")
    {
        localStorage.setItem("authorized", true);
        window.location.href = "./index.html";
    }
    else
    {
        alert("Login failed.")
        return true;
    }}

    function isAutorized(){
    localStorage.getItem("authorized")
    if (localStorage.getItem("authorized")){
        console.log(localStorage.getItem("authorized"))
        console.log("variable is TRUE");
        localStorage.setItem("authorized", false);
    }
    else
    {
        console.log(localStorage.getItem("authorized"))
        console.log("variable is FALSE");
        window.location.href = "./login.html";
    }};

var=false but condition true is running printscreen

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!