最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Password check before page loads - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a webpage that asks the user for a password prompt before the page loads. Once the correct password is typed, the page will proceed to load. If the password typed is incorrect, it will keep asking the user for the password until they get it right. The webpage currently does not ask for a password and goes right to the page.

HTML:

<html>
<head>
<title>Password Page</title>
<script type="text/javascript" src="password.js"></script>
</head>
<body onload="passwordCheck()">
<h1>How'd you know the password?</h1>
</body>
</html>

JavaScript:

// Password: ilikepie
function passwordCheck(){
var password = prompt("Please enter the password.");

if (password.value=="ilikepie"){
window.alert("Password correct!");
else{
while(password.value !="ilikepie"){
window.alert(password);}

I am trying to make a webpage that asks the user for a password prompt before the page loads. Once the correct password is typed, the page will proceed to load. If the password typed is incorrect, it will keep asking the user for the password until they get it right. The webpage currently does not ask for a password and goes right to the page.

HTML:

<html>
<head>
<title>Password Page</title>
<script type="text/javascript" src="password.js"></script>
</head>
<body onload="passwordCheck()">
<h1>How'd you know the password?</h1>
</body>
</html>

JavaScript:

// Password: ilikepie
function passwordCheck(){
var password = prompt("Please enter the password.");

if (password.value=="ilikepie"){
window.alert("Password correct!");
else{
while(password.value !="ilikepie"){
window.alert(password);}
Share Improve this question edited Sep 20, 2014 at 22:32 Bob Brown 1,5121 gold badge12 silver badges26 bronze badges asked Sep 20, 2014 at 21:49 yolotheoneyolotheone 2673 gold badges6 silver badges11 bronze badges 7
  • 4 I hope this is just an exercise and not designed to actually keep anyone out. – Gary Commented Sep 20, 2014 at 21:51
  • Well, you've hooked your JavaScript to onload so it will not run until after the page has loaded, because that's what you told it to do. I think there are more serious problems as well. – Bob Brown Commented Sep 20, 2014 at 21:55
  • 1 The syntax errors don't help either. I would suggest laying out your javascript better by indenting blocks of code. Then you can understand better what it is actually doing. – Rhumborl Commented Sep 20, 2014 at 21:55
  • erm, anyone that views the source of the page will know the password! – Phil Cross Commented Sep 20, 2014 at 21:56
  • I love that you managed to write the password in the ment too... as it wasn't visible enough already – Vland Commented Sep 20, 2014 at 21:56
 |  Show 2 more ments

3 Answers 3

Reset to default 3

Set up a login page and a destination page. The login page should prompt for the password and, when it is correct, change window.location to the real page.

function passwordCheck(){
    var password = prompt("Please enter the password.");
    if (password==="ilikepie"){
        window.location="realpage.html";
    } else{
        while(password !=="ilikepie"){
            password = prompt("Please enter the password.");
        }
        window.location="realpage.html";
    }
}
window.onload=passwordCheck;

You could actually get rid of that first check and just use the while loop. Doing that is, as we teachers say, left as an exercise.

Do pay attention to the ments about this not providing any security at all. There are (at least) two reasons why this is not secure. The first is that the password is visible to anyone who knows about "view source." The second is that, once one knows the URL of realpage.html one can navigate to it directly, bypassing the password challenge.

I assume you are doing this as an exercise, and now that you have something that executes without error, you will learn from it. The simplest effective password protocol for the web is HTTP Basic Access Authentication. When you've learned everything you can from this exercise, read about Basic Access Authentication.

The snippet of code above creates an infinite loop within the while loop due to the fact that the cancel condition of the dialog box is not checked. In order to avoid that one would need to change it as follows:

function passwordCheck(){
    var password = prompt("Please enter the password.");
    if (password==="ilikepie"){
        window.location="realpage.html";
    } else if (password!='' && password!=null) {
        while(password !=="ilikepie"){
            password = prompt("Please enter the password.");
        }
        window.location="realpage.html";
    }
}
window.onload=passwordCheck;

This will ask you the password only one time if its correct otherwise it will keep asking. this is recursive function.

window.onload=passwordCheck;
function passwordCheck()
{
    var password = prompt("Please enter the password.");
    if (password !== "bello")
    {
        passwordCheck();
    }
}
发布评论

评论列表(0)

  1. 暂无评论