0

I am trying to write a website code for something and make a username and password. I do't know if this is completely logical or if the code would work even if it was fixed, but I am a beginner and I am working from nothing. Here is what I have:

<script>
alert("Welcome. Please click ok and enter your username.")
</script>
<script>
var userName=prompt("Please enter your username:","Username")
if(userName="person") {
	alert("Next, please enter your password")
}
else{
 	reload();
}
</script>
<h1><script>
var passWord=prompt("Please enter your password:","Password")
if(passWord="person") {
	document.write("Welcome to headquarters, Person!")
}
else{
	reload();
}
</script></h1>

I am not sure how to make it work, because as of now, it always goes to the "welcome to headquarters" part. It never uses else, and it uses every username and password as right. I hope this makes sense and is possible. I would like to stick to this code as much as possible in the end since I wrote it from things I knew, not from a website. Thanks! :)

Serenity
37.1k21 gold badges125 silver badges117 bronze badges
asked Apr 27, 2015 at 1:31
2
  • 1
    Just to be sure, you're aware that this kind of username and password check is only useful for toy examples, right? Commented Apr 27, 2015 at 2:00
  • You are using = for comparison. To check if the userName is equal to "person", your code should look like if(userName=="person") { // do this } Commented Apr 27, 2015 at 2:30

1 Answer 1

2

When comparing the password you had only one '='. This assigns a value. To compare, you need two ('==').

<script>
alert("Welcome. Please click ok and enter your username.")
</script>
<script>
var userName=prompt("Please enter your username:","Username")
if(userName=="person") {
	alert("Next, please enter your password")
}
else{
 	reload();
}
</script>
<h1><script>
var passWord=prompt("Please enter your password:","Password")
if(passWord=="person") {
	document.write("Welcome to headquarters, Person!")
}
else{
	reload();
}
</script></h1>

By the way, NEVER use this for production. Anyone can view the source and see the password in plain text.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, and I am only using it for personal, fun use, not commercial or on the internet.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.