I\'m making a craft beer webpage, and I want users to type in their age before e
ID: 653876 • Letter: I
Question
I'm making a craft beer webpage, and I want users to type in their age before entering. The initial prompt works, but if I switch tabs and go back to home page I am prompted again.
Question: How do I only get one prompt and save it until user exits the whole webpage including navigating tabs.
My script:
<script>
function CloseWindow(){
window.open('','_self','');
window.close();
}
</script>
<script>
var age;
age = prompt("Please enter in your age: ");
if (age >= 21){
alert("Age is valid");
}
else{
alert("Not valid age");
CloseWindow();
}
</script>
Explanation / Answer
use localStorage to store a variable that checks whether the user has exit the whole webpage or not.
<script>
var age;
var validate = 0;
localStorage.setItem("validate", 0);
if(localStorage.getItem("validate")==0){
age = prompt("Please enter in your age: ");
if (age >= 21){
localStorage.setItem("validate", 1);
alert("Age is valid");
}
else{
alert("Not valid age");
CloseWindow();
}}
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.