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

154
Visualizações
Having trouble using localStorage

I just wanted it to greet the user when he/she comes back again and never greet the user when it's his/her first time visiting the page. Ik that the data from the localStorage could only be removed if the user manually deletes it.

Here's my code:

function hEY() {
  if (typeof(Storage) !== "undefined") {
    var yourName = document.getElementById("customer").value;
    var stored = localStorage.getItem("uname");
    if (stored != yourName) {
      localStorage.setItem("uname", yourName);
    }

    if (stored == yourName) {
      alert("Welcome back " + yourName + "!");
    } else {
      alert("You are not " + yourName + "!");
    }

  } else {
    alert("Sorry, your browser does not support Web Storage");
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The first time they go to the site, stored will be null. Check for that and don't display the greeting.

function hEY() {
  if (typeof(Storage) !== "undefined") {
    var yourName = document.getElementById("customer").value;
    var stored = localStorage.getItem("uname");
    let firstTime = stored == null;
    if (stored != yourName) {
      localStorage.setItem("uname", yourName);
    }
    if (!firstTime) {
      if (stored == yourName) {
        alert("Welcome back " + yourName + "!");
      } else {
        alert("You are not " + yourName + "!");
      }
    }

  } else {
    alert("Sorry, your browser does not support Web Storage");
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
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