For some reason after the if, it continues to else, except the ifs that take you to a different place, here is the code i have at the moment
<script type="text/javascript">
function whatname(button) {
var post = prompt("Name?", "Visitor")
if(post == "Nick"){
alert("Hi Nick")
window.location = "index.html"
}
if(post == "Visitor"){
alert("Welcome Visitor")
window.location = "index.html"
}
if(post == ""){
alert("Please Enter Name")
}
if(post == null){
alert("Closing")
}
if(post == "show me a secret"){
window.location = "secret.html"
}
if(post == "Greg"){
alert("Test")
}
if(post == "greg"){
alert("Test 1")
}
else{
alert("Welcome " + post + ", Have Fun!")
window.location = "index.html"
}
}
</script>
After the if happens, it continues to else, like let say i click cancel (which would be null) it would say, “Closing” but than another pop up would come up and would say, “Welcome Null, Have Fun!” than it would take me somewhere else, im not sure whats wrong, if you can help, it would be nice.
use
else ifrather thanifWhat’s happening now is that it is first checking if
postis “Nick”. Regardless of whether or not it is, it will move on to check ifpostis “Visitor”. This is of course not what you want. You only want to perform the second test if the first one was found to be false.As written, only the very last
ifstatement has anelse. You will get the “Welcome, have fun”-message wheneverpostis anything butgreg.