当尝试在我的网站上创建受密码保护的按钮时,错误:'('预期。给出的所有其他问题都被证明是无用的(我是javascript的初学者,因此如果答案很明显,请原谅我)
function passWord2
var askaccess = 1();
var pass1 = prompt('Please Enter Password To Access Page', ' ');
while (askaccess < 3) {
if (!pass1)
history.go(-1);
if (pass1.toLowerCase() == "gimmethedeets") {
alert('Password Correct');
window.open('logslist.html');
break;
}
askaccess += 1;
var pass1 =
prompt('Password Incorrect.', 'Password');
}
if (pass1.toLowerCase() != "password" & askaccess == 3)
history.go(-1);
return " ";
答案 0 :(得分:0)
这里可能还有 个问题,但是一个函数具有function functionname(variablename){}
签名。
这些都不是安全的密码输入,用户可以轻松查看/猜测。
function passWord2() {
var askaccess = 1();
var pass1 = prompt('Please Enter Password To Access Page', ' ');
while (askaccess < 3) {
if (!pass1)
history.go(-1);
if (pass1.toLowerCase() == "gimmethedeets") {
alert('Password Correct');
window.open('logslist.html');
break;
}
askaccess += 1;
var pass1 =
prompt('Password Incorrect.', 'Password');
}
if (pass1.toLowerCase() != "password" & askaccess == 3)
history.go(-1);
return " ";
}
// call it
passWord2();
重写一些意图。在适当的地方发表评论
function checkForPassword() {
// declare stuff, use let to keep block scope,
// these have values here to not get reference error
// not strickly needed but a good habit,
// when it gets a value read:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
let entryAttempts = 1,
entryValue = "",
correctValue = "gimmethedeets";
// this gives 2 attempts due to the 1 initial value and not 0
while (entryAttempts < 3) {
entryValue = prompt('Please Enter Password To Access Page', '');
// check for null empty values
if (entryValue != null && entryValue.length > 0 && entryValue.trim() != "") {
// I find this slightly more clear but same as history.go(-1);
window.history.back();
}
// value null if they hit cancel, protect from that
if (entryValue != null && entryValue.toLowerCase() == correctValue) {
alert('Password Correct');
// open the new window, different than making THIS page go there as ino window.location ='logslist.html';
window.open('logslist.html');
break;
}
entryValue = prompt('Password Incorrect.', 'Password');
entryAttempts += 1; // last in while loop
}
// slight restructure to make intent clear, fix bitwise & to to logical &&
// a guess on the intent here
if ((entryValue == null || entryValue.toLowerCase() != "password") && entryAttempts === 3) {
history.back();
} else {
// to be clear if entry is "Password" it returns after 2 attempts
return " ";
}
}
// call it
let entry = checkForPassword();