Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote