Global variables/array question
Users = new Array;
Passwords = new Array;
function LogIn() {
Users[10] = "username"
Passwords[10] = "password"
Username = user.value;
Password = pass.value;
for (i = 0; i <= Users.length; i++) {
if (Users[i] == Username) {
if (Passwords[i] == Password) {
alert("yay!");
} else
{
alert("nay");
}
}
}
}
function Register() {
Username = user.value;
Password = pass.value;
Users.push(Username);
Passwords.push(Password);
}
Alright, so I'm teaching myself Javasctipt in my free time and I decided that the best way would be to just mess around with it for a while. I am trying, currently, to build a primative "log in"/"register" webpage/function and I've obviously run into a few problems.
Global variables. I need the arrays "Users" and "Passwords" to be global, but the way I have it set up now, I think they are initializ开发者_StackOverflow社区ed every single time I call the function-set. So, I guess I'll ask both my questions like this: I realize that arrays probably aren't the best thing for a project like this, however, how do I get the values I store in the arrays to persist from run to run?
<script type="text/javascript" src="LogIn.js"></script>
<script type="text/javascript" src="Register.js"></script>
<body>
Username: <input type="text" id="user" />
Password: <input type="password" id="pass" />
<input type="button" value="Log In" onClick="LogIn()"/>
<input type="button" value="Register" onClick="Register()" />
<hr />
</body>
It was kind of difficult to understand what you're asking for, but I think this will point you in the right direction:
Users = new Array;
Passwords = new Array;
Users[0] = "john";
Users[1] = "sue";
Users[2] = "jack";
Passwords[0] = "blue";
Passwords[1] = "black";
Passwords[2] = "green";
function LogIn() {
//login logic here
}
pretty close
Users = new Array();
Passwords = new Array();
function LogIn() {
Username = document.getElementById("user").value;
Password = document.getElementById("pass").value;
for (i = 0; i <= Users.length; i++) {
if (Users[i] == Username) {
if (Passwords[i] == Password) {
alert("yay!");
} else
{
alert("nay");
}
}
}
}
function Register() {
Username = document.getElementById("user").value;
Password = document.getElementById("pass").value;
Users.push(Username);
Passwords.push(Password);
}
just need a little tweaking.
This question is a bit hard to understand. Web pages are stateless and values stored in javascript will only persist until the page is reloaded.
For your experiment, if you wanted to do some type of persistence across page loads you should look at something like using cookies.
精彩评论