I have a javascript prompt that executes when I open my website for the first time. The basic function of this prompt is to ask the user for their name, and then insert it into a header I.E. "Hello, 'user'!" I have multiple pages on my website, and whenever I navigate to a different page and then navigate back to the home page, the prompt is displayed again, asking the user the same question over and over. Is there any way to have the prompt display only once on the first load of the website, and then keep the user's name in a variable until they close the website?
Here is my javascript:
function askName() {
let username = prompt("To make your time on this website better, please enter your name.");
if (username != null) {
document.getElementById("userpara").innerHTML = "Hello, " + username;
} else {
document.getElementById("noUser").innerHTML = "Wele, Stranger!";
}
}
And here is my Html:
<h1 id="userpara" style="text-align: center;"></h1>
<h1 id="noUser" style="text-align: center;"></h1>
To execute the function on page load, I used the onload
function in the body tag in html like this:
<body onload="askname()">
...
</body>
Feel free to correct me on any of my code as well, since I'm still learning :)
I have a javascript prompt that executes when I open my website for the first time. The basic function of this prompt is to ask the user for their name, and then insert it into a header I.E. "Hello, 'user'!" I have multiple pages on my website, and whenever I navigate to a different page and then navigate back to the home page, the prompt is displayed again, asking the user the same question over and over. Is there any way to have the prompt display only once on the first load of the website, and then keep the user's name in a variable until they close the website?
Here is my javascript:
function askName() {
let username = prompt("To make your time on this website better, please enter your name.");
if (username != null) {
document.getElementById("userpara").innerHTML = "Hello, " + username;
} else {
document.getElementById("noUser").innerHTML = "Wele, Stranger!";
}
}
And here is my Html:
<h1 id="userpara" style="text-align: center;"></h1>
<h1 id="noUser" style="text-align: center;"></h1>
To execute the function on page load, I used the onload
function in the body tag in html like this:
<body onload="askname()">
...
</body>
Feel free to correct me on any of my code as well, since I'm still learning :)
Try using sessionStorage
:
function askName() {
let username = sessionStorage.getItem('username');
if (username === null) {
username = prompt("To make your time on this website better, please enter your name.");
}
if (username != null) {
document.getElementById("userpara").innerHTML = "Hello, " + username;
sessionStorage.setItem('username', username);
} else {
document.getElementById("noUser").innerHTML = "Wele, Stranger!";
}
}
sessionStorage
is different from localStorage
in that sessionStorage
is wiped whenever you close the window/tab, which is what you want. localStorage
stays until a user clears the cache or you remove the data through script.
There are a variety of ways to persistently store information on a client's web browser:
Cookies
Probably the easiest way to do this is by using localStorage:
function askName() {
let username = localStorage.getItem('username');
if (!username) {
username = prompt("To make your time on this website better, please enter your name.");
}
if (username != null) {
document.getElementById("userpara").innerHTML = "Hello, " + username;
localStorage.setItem('username', username);
} else {
document.getElementById("noUser").innerHTML = "Wele, Stranger!";
}
}
Note that the information doesn't get cleared upon closing the browser (probably the closest thing to do that is a session cookie, but it es with the concerns of using cookies). Alternatively you can just use sessionStorage, if you don't need other tabs/windows accessing the same data.
save the value to localStorage and check next time if value there in localStorage
function askName() {
var user=localStorage.getItem('Username');
if(user){
document.getElementById("userpara").innerHTML = "Hello, " + user;
}
else{
let username = prompt("To make your time on this website better, please enter your name."); if (username != null) {
user=localStorage.setItem('Username', username)
document.getElementById("userpara").innerHTML = "Hello, " + username;
} else {
document.getElementById("noUser").innerHTML = "Wele, Stranger!";
}} }