Javascript密码提示错误

时间:2011-12-16 13:51:56

标签: javascript

我似乎无法弄清楚如何纠正我遇到的两个问题。

以下是我正在使用的代码:

function password() {
    var testV = 1;
    var pass1 = prompt('Please Enter The Password','');
    while (testV < 2) {
        if (!pass1) 
            history.go(-1);
        if (pass1 == "default") { //password here
            window.open('http://www.[redacted].com/downloads/documents/12-2011-project.psd'); //if password correct
            break;
        } 
        testV += 1;
        var pass1 = 
            prompt('Incorrect - Please Try Again.',''); //if password incorrect
    }
    if (pass1 != "password" & testV == 2)               
        history.go(-1);
    window.location = "/exit.html"; //redirects to here after success and opens download
}

我的第一个问题: 单击“取消”时,会再次提示。我只是希望它在我点击取消后消失。

第二 单击“取消”后使用2次尝试后,它会重定向,就像下载一样(没有激活下载,这很好。)

我做错了什么,或者需要修理什么?

此外,如果用户只按两次“确定”,则不会激活下载,但仍会重定向。我在退出页面上留下了一条说明这一点的说明。这不会让我感到烦恼,但可以修复吗?如果不是那样对我没问题。

已解决(12月16日上午9:30) 感谢所有回答我的第一个问题!

4 个答案:

答案 0 :(得分:0)

这是我试图'修复'它的尝试:

function password() {
    var counter = 0;
    var promtText = {'Please Enter The Password', 'Incorrect - Please Try Again.'};
    while (counter < 2) {
       var passw = prompt(promtText[counter],'');
        if (passw == "default") { //password here
            window.open('http://www.[redacted].com/downloads/documents/12-2011-project.psd'); //if password correct
            break;
        } 
        counter++;
    }
    if (passw == "password")               
        window.location = "/exit.html"; //redirects to here after success and opens download
    else
        history.go(-1);
}

这里似乎有一些关于正确密码是什么的混淆:)

此外,您不应该依赖客户端验证,特别是与安全相关的任何事情。

答案 1 :(得分:0)

问题1 - 如果我理解正确,如果用户取消您想要中止所有进一步的处理,那么您只需通过更改此内容即可从password()功能返回:

if (!pass1) 
  history.go(-1);

对此:

if (!pass1) {
  history.go(-1);
  return;
}

否则,如果你想留在你的函数中说break而不是return(正如你已经为正确的密码所做的那样)退出while循环然后添加适当的if结构循环后的那种情况。

问题2 - 如果您已经退出password()功能,则不会发生重定向,如上所示。如果您正在尝试满足用户未取消但密码错误的情况,请执行以下操作:

if (pass1 != "password" & testV == 2)               
    history.go(-1);
else
    window.location = "/exit.html";

虽然我不确定你为什么要测试testV的价值。

注意:如果您的JavaScript中有密码,则用户可以通过查看您的JS源来查看密码。

答案 2 :(得分:0)

只需检查pass1是否为真,因为取消表单时不是这样。 您还必须在最后一个if子句中询问!pass1。 这是我的尝试:

function password() {
var testV = 1;
var pass1 = prompt('Please Enter The Password','');
while (testV < 2 && pass1) {
    if (!pass1)
        history.go(-1);
    if (pass1 == "default") { //password here
        window.open('http://www.[redacted].com/downloads/documents/12-2011-project.psd'); //if    password correct
        break;
    }
    testV += 1;
    var pass1 =
        prompt('Incorrect - Please Try Again.',''); //if password incorrect
}
if (pass1 != "password" & testV == 2 & !pass1) history.go(-1);
window.location = "/exit.html"; //redirects to here after success and opens download
}

答案 3 :(得分:0)

这应该有效。请注意变量和硬编码真实密码和尝试次数

另请注意,当取消promt窗口时,它会返回null,因此您应该检查pass1 != null

function password() {
    var real_pass = "default";
    var num_of_tries = 4;
    var pass1 = prompt('Please Enter The Password','');
    var testV = 1;

    if(pass1 != null) {
        if (pass1 == real_pass) {
            window.open('http://www.[redacted].com/downloads/documents/12-2011-project.psd'); //if password correct
        }
        else {
            while (testV < num_of_tries && pass1 != null) {
                var pass1 = prompt('Incorrect - Please Try Again.','');             
                if (pass1 == real_pass) {
                    window.open('http://www.[redacted].com/downloads/documents/12-2011-project.psd'); //if password correct
                    break;
                }
                testV++;

            }
        }
    }
    history.go(-1);
    window.location = "/exit.html"; //redirects to here after success and opens download
}