[英]Problem with comparing value with array values
这段代码就是我现在使用的。 但是当我尝试使用数组来比较值时,它不起作用。 如果有人知道为什么,请回复。
<html>
<head>
<script type-'text/javascript'>
function hovedFunksjon()
{
//alert("test av funksjon fungerte");
//alert(passordLager);
window.open("index10.html","Window1","menubar=no,width=430,height=360,toolbar=no");
}
function inArray(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}
return false;
}
function spørOmPassord()
{
var passordLager = ["pass0","pass1","pass2"];
window.passordInput = prompt("password");//Ved å bruke "window." skaper man en global variabel
//if (passordInput == passordLager[0] || passordLager[1] || passordLager[2])
if (inArray(passordLager,passorInput) )
{
hovedFunksjon();
}
else
{
alert("Feil passord");
//href="javascript:self.close()">close window
}
}
function changeBackgroundColor()
{
//document.bgColor="#CC9900";
//document.bgColor="YELLOW"
document.bgColor="BLACK"
}
</script>
</head>
<body>
<script type-'text/javascript'>
changeBackgroundColor();
</script>
<div align="center">
<form>
<input type = "button" value = "Logg inn" onclick="spørOmPassord()">
</form>
</div>
</body>
</html>
if (array[i] == value) return true;
}
return false;
}
那真的是误导性的缩进!
window.passordInput = prompt("password");
我不确定为什么你使用全局来存储输入,因为你只在本地函数中使用它。 (如果你真的需要一个全局,你不需要window.
前缀,因为你还没有用本地var
声明该变量。)
这可能是你的问题: prompt
在IE7 +中不再可用。 由于(非常可疑)安全原因,微软已经停止了它的工作。 您可能需要提出另一种涉及表单字段的方法,例如:
<input id="password" type="password"/>
<input id="login" type="button" value="Login"/>
<script type="text/javascript">
document.getElementById('login').onclick= function() {
var password= document.getElementById('password').value;
if (['pass0', 'pass1', 'pass2'].indexOf(password)!==-1) {
window.open('thing.html', '_blank');
} else {
alert('no.');
}
};
</script>
我在这里使用Array#indexOf
进行列表内测试。 这是新版JavaScript中的标准方法,但并非每个浏览器都支持它。 您可以将其添加到不支持的浏览器中,如下所示:
// Add ECMA262-5 Array indexOf if not supported natively
//
if (!('indexOf' in Array.prototype)) {
Array.prototype.indexOf= function(find, from /*opt*/) {
for (var i= from || 0, n= this.length; i<n; i++)
if (i in this && this[i]===find)
return i;
return -1;
};
}
无论哪种方式,请注意JavaScript密码“保护”不仅对可访问性而言非常糟糕,而且完全不安全 。 你永远不应该在任何你关心的事情上使用它。 如果要正确执行,请查找真正的HTTP基本身份验证(htaccess)和/或基于cookie的表单登录。
你刚刚在passorInput
忘记了一个d
:
inArray(passordLager,passordInput)
您分配给window.passordInput
但传递passordInput
- 它们不相同。
如果在FireFox中打开它,您可以通过工具 - >错误控制台查看脚本执行生成的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.