Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
Opening a local HTML page through javascript

I have this html code where the index.html loads first. It has a text field and a submit button. When the submit button is clicked, it calls the validateForm() function. Suppose the text field is empty, it should alert that it is empty, if it isn't then a new html page would load (either in new tab or current one) which is supposed to be welcome.html

So my problem is, the welcome.html file isn't loading. I even changed the "welcome.html" in window.location.href = "welcome.html" to "https://www.google.com" . The page still wouldn't load. What's wrong and what am I missing here ?

Any help is appreciated.

index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Form</title>
    
        <script src="/validateForm.js"></script>
    </head>
    <body>
        <form method="get" onsubmit="return validateForm()">
            Name: <input type="text" name="name" id="name">
            <input type="submit" value="Submit">
        </form>
    </body>
    </html>

validateForm.js

function validateForm() {
    var name = document.getElementById("name");
    var nameData = name.value;

    if (nameData == "" || nameData == null) {
        alert("Name must be filled!");
    }
    else {
        window.location.href = "welcome.html";
    }
}

welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome :)</title>
</head>
<body>
    <h1>Welcome!</h1>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use action attribute

You can use the action attribute on the form to forward to a URL on success:

<form method="get" action="welcome.html" onsubmit="return validateForm()">
    Name: <input type="text" name="name" id="name">
    <input type="submit" value="Submit">
</form>

Documentation: MDN - Form action attribute

return value of validateForm()

See this?
onsubmit="return validateForm()"

Your validateForm doesn't return anything.
It is convention that return false means "stop processing the form" and a true-ish value means continue, which in turn means the action attribute mentioned above leads to welcome.html as you wanted.

The modified function could be this:

function validateForm() {
    var name = document.getElementById("name");
    var nameData = name.value;

    if (nameData == "" || nameData == null) {
        alert("Name must be filled!");
        return false;
    }
    
    return true;
}

Alternatively, give validateForm a first parameter named event and call event.preventDefault() instead of returning false.

Documentation: MDN - Form submit handling example

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need some changes in index.html file and validateForm.js file

index.html

<form method="get" onsubmit="return validate()" action="javascript::window.location.replace('welcome.html')">
...
</form>

and in validateForm.js you need to return true or false after validation function if true it redirects to welcome.html or if false nothing were heppened.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda