I'm trying to create a personalized welcome message on the homepage, that basically says "welcome [username]" providing the user has registered and their username goes into localStorage. That works fine, but I only want the message to display the username if the user has actually registered, (otherwise it says "welcome Null". So I'm trying to use the following code to do that:
// Stores username inputted on registration page inside Local Storage//
function storeUsrName() {
var regName= document.getElementById("regName");
localStorage.setItem("usrName", regName.value);
alert("Thanks for registering!");
};
// Display welcome message //
$(document).ready(function() {
var usrName = localStorage.getItem("usrName");
if (usrName !="null" || usrName !="undefined") {
document.getElementById("welcomeMessage").innerHTML = ("Welcome " + usrName + "! ");
} else {
document.getElementById("welcomeMessage").innerHTML = ("Welcome! ");
}
});
If a user registers and usrName is assigned a value, the welcome message works fine. But I expect the welcome message to say "Welcome!" if its the users first time visiting the site, and they have yet to register, and usrName has no value (null, or undefined).
Instead, if usrName has no value, the if/else statement doesn't work correctly, and the welcome message displays "welcome null!", despite the fact that I only want usrName to be displayed in the welcome message IF the value is NOT null or undefined.
Is there some correct way of doing this?