Load webpage in JS condition

I have a problem with the following. A person must enter his age and lastname and then the js code must check it. If the age >39 a webpage must be loaded where the user will continue with regsitration. If not, display an alert that he is too young “Condition not met”. If the user age <39 the alert is dispplayed but if the user => 39 nothing happens.
I am sure I call the website incorrectly!

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="" onSubmit="myFunction()">
    <label for="age">Your Age:</label><br>
    <input type="text" id="age" name="age" value=""><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value=""><br><br>
    <input type="submit" value="Submit">
</form> 
<script type="text/javascript">
   function myFunction(){
     var age = document.getElementById("age").value;
     if(age > 39) {
        window.location.assign("http://net-informations.com"); 
    }    
	else
        alert("Condition Not met");
   }
</script>

Help would be appreciated.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Sorry for the oversight

Thank you for rectifying it

Kind regards

Hello and welcome to the fCC community~!

Are you getting any errors in your console?

Hi No in console only an > is displayed

Okay, I found the issue (at least, on my local test instance).

HTML forms, by default, refresh the page on a submit action. This interferes with your window.location.assign() call. The trick to bypass this is to change your onSubmit action to:

<form action="" onSubmit="myFunction(); return false">

This will prevent the default refresh event.

Hi
Thank you. I have changed my form action as you suggested now I get a console error:Not allowed to load local resource: file:///C:/LoadWebsite.html

That sounds like you’ve added a link to a file in your code but haven’t configured the relative path appropriately. Have you changed anything else in your code?

You might have to serve the page.

If you use VS Code you can install something like the Live Server extension to serve the page.

1 Like

Thank you for all your help. It is now working fine.
I really appreciate all the input from members!