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

148
Vistas
Javascript infinite loop in prompt

So i have just made a simple HTML page in which a JS script runs when the page loads. But the problem is that it just goes infinite after asking password. I tried to find some solutions but failed to do the same. Please help. Below is the code.

<!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>Alert test</title>
</head>
<body onload="alert()">
<script>
    function alert() {
        var uname, pass, corr_uname = "admin", corr_pass = "admin";
        while(!uname) {
            uname = prompt("Enter Username: ");
        }
        while(!pass) {
            pass = prompt("Enter Password: ");
        }
        if((uname == corr_uname) && (pass == corr_pass)) {
            alert("Access Granted!!");
        } else {
            alert("Access Denied!");
            alert();
        }
    }
</script>
<h1>Welcome!</h1>
</body>
</html>

The funny thing is that when i run the same code (JS runs after clicking a button) in W3Schools, it just works fine!!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is that you have created a function named alert which already exists in javascript, so you are calling it recursively and infinitely.

Solution fix

<!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>Alert test</title>
</head>
<body onload="alert2()">
<script>
    function alert2() {
        var uname, pass, corr_uname = "admin", corr_pass = "admin";
        while(!uname) {
            uname = prompt("Enter Username: ");
        }
        while(!pass) {
            pass = prompt("Enter Password: ");
        }
        if((uname == corr_uname) && (pass == corr_pass)) {
            alert("Access Granted!!");
        } else {
            alert("Access Denied!");
            myFunction();
        }
    }
</script>
<h1>Welcome!</h1>
</body>
</html>

I renamed your functional alert to alert2.

Kindly accept it as answer if it works for you, thanks!

about 4 years ago · Juan Pablo Isaza Denunciar

0

JavaScript had mutable built-in functions so your alert function overwrites the native alert and every alert call becomes recursive in a never ending loop.

Besides renaming the function, there's no need to use a while loop since prompt stalls execution. You can use uname && to invalidate the username if the user cancels the username prompt.

There's also no need to use <body onload="??"> if you just put the script within <head>...</head>

<!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>Alert test</title>
<script>
    const corr_uname = "admin", corr_pass = "admin";
    
    function authenticate() {        
        const uname = prompt("Enter Username: ");
        const pass = uname && prompt("Enter Password: ");

        const isCredentialsValid = uname == corr_uname && (pass == corr_pass);
        const accessType = isCredentialsValid ? 'Granted' : 'Denied';

        alert(`Access ${accessType}!!`);
    }
    authenticate();
</script>
</head>
<body>
<h1>Welcome!</h1>
</body>
</html>
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