admin管理员组

文章数量:1426072

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("Wele 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("Wele " + 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 e up and would say, "Wele Null, Have Fun!" than it would take me somewhere else, im not sure whats wrong, if you can help, it would be nice.

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("Wele 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("Wele " + 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 e up and would say, "Wele Null, Have Fun!" than it would take me somewhere else, im not sure whats wrong, if you can help, it would be nice.

Share Improve this question edited Aug 21, 2012 at 14:57 Kuf 17.8k7 gold badges68 silver badges91 bronze badges asked Aug 21, 2012 at 14:55 bebarules666bebarules666 892 gold badges2 silver badges5 bronze badges 1
  • use else if that will help... – Scorpio Commented Aug 21, 2012 at 14:58
Add a ment  | 

8 Answers 8

Reset to default 8

use else if rather than if

if(condition 1) {
} else if(condition 2) {
} else {
}

What's happening now is that it is first checking if post is "Nick". Regardless of whether or not it is, it will move on to check if post is "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 if statement has an else. You will get the "Wele, have fun"-message whenever post is anything but greg.

Because the else is only applying to your last if.

<script type="text/javascript">
    function whatname(button) {
        var post = prompt("Name?", "Visitor")
        if(post == "Nick"){
            alert("Hi Nick")
            window.location = "index.html"
        }
        else if(post == "Visitor"){
            alert("Wele Visitor")
            window.location = "index.html"
        }
        else if(post == ""){
            alert("Please Enter Name")
        }
        else if(post == null){
            alert("Closing")
        }
        else if(post == "show me a secret"){
            window.location = "secret.html"
        }
        else if(post == "Greg"){
            alert("Test")
        }
        else if(post == "greg"){
            alert("Test 1")
        }
        else{
            alert("Wele " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
</script>

This way it will only execute one of the blocks of code.

Or better, change to use a switch statement http://jsfiddle/qsQvs/

<script type="text/javascript">
function whatname(button) {
    var post = prompt("Name?", "Visitor");

    switch (post) {
    case "Nick":
        {
            alert("Hi Nick");
            window.location = "index.html";
            break;
        }
    case "Visitor":
        {
            alert("Wele Visitor");
            window.location = "index.html";
            break;
        }
    case "":
        {
            alert("Please Enter Name");
            break;
        }
    case "show me a secret":
        {
            window.location = "secret.html";
            break;
        }
    case "Greg":
        {
            alert("Test");
            break;
        }
    case "greg":
        {
            alert("Test 1");
            break;
        }
    default:
        {
            alert("Wele " + post + ", Have Fun!");
            window.location = "index.html";
        }
    };
}
</script>

In case only ONE on the IFs must happen (or the else), the you must use else if. Right now, the else only applies to the "greg" IF.

if(post == "Nick"){
    alert("Hi Nick")
    window.location = "index.html"
}
else if(post == "Visitor"){
    alert("Wele Visitor")
    window.location = "index.html"
}
else if(post == ""){
    alert("Please Enter Name")
}
else if(post == null){
    alert("Closing")
}
else if(post == "show me a secret"){
    window.location = "secret.html"
}
else if(post == "Greg"){
    alert("Test")
}
else if(post == "greg"){
    alert("Test 1")
}
else{
    alert("Wele " + post + ", Have Fun!")
    window.location = "index.html"
}

Be careful!

Your else currently only triggers if the very last if does not. Whether or not any of the other ifs trigger, it doesn't care.

If you know it should only be one of these that ever fires, put elses before each if:

if(post == "Nick"){
    alert("Hi Nick")
    window.location = "index.html"
}
else if(post == "Visitor"){   //Now this will only be considered if the first failed
    alert("Wele Visitor")
    window.location = "index.html"
}

That way, the final else will only be considered if all else failed

Currently you have 7 different conditional blocks and the else is only part of the last one. In this case, since what you really want is 1 conditional block with 8 branches, you should be using else if instead of if on the middle 6:

if(post == "Nick") {
   alert("Hi Nick")
   window.location = "index.html"
} else if(post == "Visitor") {
    alert("Wele Visitor")
    window.location = "index.html"
}

    [...]

else {
    alert("Wele " + post + ", Have Fun!")
    window.location = "index.html"
}

Also, since you are paring the same variable to a bunch of different things, I would prefer to see this written as a switch, but that is a matter of taste:

switch(post)
{
    case "nick":
        alert("Hi Nick")
        window.location = "index.html"
        break;

    [...]

    default:
        alert("Wele " + post + ", Have Fun!")
        window.location = "index.html"
        break;
}

An else block is tied only to its immediately-preceding if, which means that all of your other if blocks are independent of one another. Use else if to create several mutually exclusive options.

Note, however, that you won't find any mention of else if in the JavaScript (ECMAScript) spec. It's not its own thing. But each else is associated with the nearest possible if that has no else, which allows you to string multiple conditions together.

In other words, this:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else if (post == "Visitor") {
    alert("Wele Visitor")
    window.location = "index.html"
}
else if (post == "") {
    alert("Please Enter Name")
}
else {
    alert("Wele " + post + ", Have Fun!")
    window.location = "index.html"
}

...is equivalent to this:

if (post == "Nick") {
    alert("Hi Nick")
    window.location = "index.html"
}
else {
    if (post == "Visitor") {
        alert("Wele Visitor")
        window.location = "index.html"
    }
    else {
        if (post == ""){
            alert("Please Enter Name")
        }
        else {
            alert("Wele " + post + ", Have Fun!")
            window.location = "index.html"
        }
    }
}

i think you made a small mistake try this code:

function whatname(button) {
    var post = prompt("Name?", "Visitor")

    if(post == "Nick"){
        alert("Hi Nick")
        window.location = "index.html"
    }
    else if(post == "Visitor"){
        alert("Wele Visitor")
        window.location = "index.html"
    }
    else if(post == ""){
        alert("Please Enter Name")
    }
    else if(post == null){
        alert("Closing")
    }
    else if(post == "show me a secret"){
        window.location = "secret.html"
    }
    else if(post == "Greg"){
        alert("Test")
    }
    else if(post == "greg"){
        alert("Test 1")
    }
    else{
        alert("Wele " + post + ", Have Fun!")
        window.location = "index.html"
    }
}

This looks like it would be much better suited in a switch statement, this would increase maintainability dramatically.

 var post = prompt("Name?", "Visitor") 
 switch(post)
 {
 case "Nick":
        alert("Hi Nick")  
        window.location = "index.html";  
        break;
 case "Visitor":
        alert("Wele Visitor")  
        window.location = "index.html";
        break;  
 case "":
        alert("Please Enter Name") ;
        break;
 case null:
        alert("Closing");  
        break; 
 case "show me a secret":
        window.location = "secret.html";  
        break;
 case "Greg":  
        alert("Test");  
        break;
 case "greg":
        alert("Test 1")  
        break;    
 default:
        alert("Wele " + post + ", Have Fun!")  
        window.location = "index.html";    
 }

本文标签: javascriptafter if continues to elseStack Overflow