Help with a (seemingly) simple problem?

Axle12693

New Member
Hey, I'm trying to make a web page with just a basic javascript login form, but something isn't working, and I don't know what. Please, don't post saying that the form is too simple, or that I should just do it with PHP or some other server-side language. It's just a test. All I want is to know what I am doing wrong.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Test JS Login</title>
<script type="text/javascript">
function Login(){
password = document.password
if (password.value == "yb6"){
link = document.getElementById("link")
link.href = "mypage.html"
}
else{
document.write("Wrong.")
}
}
</script>
</head>

<body>
<p align="center">
Please enter the password.
<br/>
<input name="password" type="password">
<br/>
<input type="button" value="Log in" onClick="Login()">
</p>
<a href="http://www.google.com" id="link">Enter the password to view my page.</a>
</body>
</html>
 

jnjc

New Member
Try this:

Code:
function Login(){
[B]var[/B] password = [B]document.getElementById("password");[/B]
if (password.value == "yb6"){
[B]var[/B] link = document.getElementById("link")[B];[/B]
link.href = "mypage.html"[B];[/B]
}
else{
document.write("Wrong.")[B];[/B]
}
}

Without having spent too much time looking at this I am guessing the main reason your version wasn't working was because the line:

Code:
password = document.password

Was trying to assign the password object to itself, by putting the 'var' in front of it, it defines a new "password" variable, scoped to the function.

Another way to do it would be as follows:

Code:
function Login(){
if (document.getElementById("password").value == "yb6") 
                    document.location = "mypage.html";
else document.write("Wrong.");
}

This will direct you automatically to the page on successful login, it also cuts out some of the code you don't need.

HTH,
JC
 
Top