J. Martinez J. Martinez - 11 days ago 5
HTML Question

How can I insert an HTML file into a JavaScript file?

So, I have a webpage where the user needs to input their age, if they're older than 18 years old, the screen will display "Welcome to the page!" otherwise, it will display "You're not old enough". I have all done, but I made another HTML page where the header says "Welcome to the page!". I want to link that HTML new file, and I want to load it after the user inputs their age, and if they're older than 18, I want that HTML file to come up.
I have the following code:

function yourAge() {

var age = prompt("Please enter your age:");

if (!age) {
alert("Please fill in all the required field!");
return false;
}

var regex=/^[0-9]+$/;
if (!age.match(regex))
{
alert("Must input numbers!");
return false;
}


if (age >= 18) {
document.write("Welcome to the website!");
}

else {
document.write("You're not old enough to enter this website!")
}

}


I think I would remove the "document.write" but I'm not sure what to do in there...

If I didn't make myself clear, I basically want to redirect the user to that HTML file once their input their age, and only display it if they're older than 18, otherwise, I will create another one that says "You're not old enough".

Answer

You could use document.location to redirect after checks:

if (age >= 18) {
    document.write("Welcome to the website!");
    document.location = "YOUR_PAGE_URL.html";
}