I can’t seem to figure out how to correct the 2 problems I’m running into.
Here is the code I am working with:
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
}
My first problem:
When clicking “Cancel” it prompts again. I just want it to simply disappear after I click cancel.
Second:
After the 2 attempts are used when you click “Cancel“, it redirects as if downloaded (no download is activated, which is good.)
What am I doing wrong, or what needs to be fixed?
Also, If a user just presses “OK” twice, there is no download activated, but it still redirects. I left a note on the exit page stating this. This doesn’t bug me, but can that be fixed?; if not that’s fine with me.
SOLVED (12/16 @ 9:30 AM)
Thanks to all that answered my first question!.
Problem 1 – If I understand you correctly, if the user cancels you want to abort all further processing so you can simply return from the
password()function by changing this:To this:
Otherwise if you want to stay in your function say
breakinstead ofreturn(as you already do for the correct password) to get out of the while loop and then add the appropriate if structure for that situation after the loop.Problem 2 – No redirect will occur if you’ve already returned out of your
password()function as shown above. If you’re trying to cater for when the user has not cancelled but has gotten the password wrong then something like this:Though I’m not sure why you’re testing the value of
testVat that point.Note: If you’ve got passwords in your JavaScript the user will be able to see the passwords by viewing your JS source.