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");
}
}
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");
}
}