简体   繁体   English

Javascript 无限循环提示

[英]Javascript infinite loop in prompt

So i have just made a simple HTML page in which a JS script runs when the page loads.所以我刚刚制作了一个简单的 HTML 页面,其中在页面加载时运行 JS 脚本。 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!!有趣的是,当我在 W3Schools 中运行相同的代码(JS 在单击按钮后运行)时,它工作正常!

The problem is that you have created a function named alert which already exists in javascript, so you are calling it recursively and infinitely.问题是您已经创建了一个名为 function 的alert ,它已经存在于 javascript 中,因此您正在递归和无限地调用它。

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 .我将您的功能alert重命名为alert2

Kindly accept it as answer if it works for you, thanks!如果对您有用,请采纳为答案,谢谢!

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. JavaScript 具有可变的内置函数,因此您的alert function 会覆盖本机警报,并且每个警报调用都会在永无止境的循环中递归。

Besides renaming the function, there's no need to use a while loop since prompt stalls execution.除了重命名 function 之外,不需要使用 while 循环,因为提示会停止执行。 You can use uname && to invalidate the username if the user cancels the username prompt.如果用户取消用户名提示,您可以使用uname &&使用户名无效。

There's also no need to use <body onload="??"> if you just put the script within <head>...</head>如果您只是将脚本放在<head>...</head>中,也无需使用<body onload="??"> >

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM