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

151
Views
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 answers
Answer question

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 Report
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!