function passWord() {
var pass1 = prompt('Please Enter Your Password',' ');
if (!pass1) {
window.location.href = ('bad.html');
}
else if (pass1.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
break;
}
else {
var pass2 = prompt('Password Incorrect','Password');
if (!pass2) {
window.location.href = ('bad.html');
}
else if (pass2.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
break;
}
else {
var pass3 = prompt('Incorrect, last try','Password');
if (!pass3) {
window.location.href = ('bad.html');
}
else if (pass3.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
break;
}
else {
window.location.href = ('bad.html');
}
}
}
}
window.onload = passWord()

我正在尝试创建一个脚本,以便它要求输入密码三次,如果它们都不正确,它会将您带到“坏”的#34;页。 但是,该脚本似乎不起作用。 我不知道我的代码有什么问题...... 我不应该在其他地方有ifs和elses吗?
答案 0 :(得分:0)
您只需要将break
语句切换为return
。一旦你想离开这个方法。 break
用于保留循环块。
function passWord() {
var pass1 = prompt('Please Enter Your Password',' ');
if (!pass1) {
window.location.href = ('bad.html');
}
else if (pass1.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
var pass2 = prompt('Password Incorrect','Password');
if (!pass2) {
window.location.href = ('bad.html');
}
else if (pass2.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
var pass3 = prompt('Incorrect, last try','Password');
if (!pass3) {
window.location.href = ('bad.html');
}
else if (pass3.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
window.location.href = ('bad.html');
}
}
}
}
window.onload = passWord()

答案 1 :(得分:0)
function passWord() {
var pass1 = prompt('Please Enter Your Password', '');
if (!pass1) {
window.location.href = ('bad.html');
}
else if (pass1.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
var pass2 = prompt('Password Incorrect', 'Password');
if (!pass2) {
window.location.href = ('bad.html');
}
else if (pass2.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
var pass3 = prompt('Incorrect, last try', 'Password');
if (!pass3) {
window.location.href = ('bad.html');
}
else if (pass3.toLowerCase() == "yep") {
window.location.href = ('webpage.html');
return;
}
else {
window.location.href = ('bad.html');
}
}
}
}
window.onload = function() {
passWord();
}
我在这里丢失了你的缩进,但上面的代码看起来像你想要的效果。
代码未加载,因为您没有正确调用window.onload函数。由于“休息”,条件在被调用后无法正常工作。
答案 2 :(得分:0)
我发现了自己,显然我不需要任何破坏标签。 我删除了所有内容,现在我的代码正常运行感谢所有花时间回答我的问题的人,并且因为询问这样一个简单的错误而道歉