Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda