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

156
Visualizações
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 à 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