Problems a Javascript else/if statement and variables.

Discussion in 'JavaScript' started by JSellnau_TSCC, Mar 27, 2010.

  1. #1
    <script type="text/javascript">
    <!--
    var areaname = "<?php echo $arename; ?>";
    
    if(areaname = "listings-residential"){
        document.location.href="residential.php";
    } else {
        document.location.href="commercial.php";
    }
    //-->
    </script>
    Code (markup):
    This is the code I am using and regardless of what info is in the variable it always re-directs to the first link.
     
    JSellnau_TSCC, Mar 27, 2010 IP
  2. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <script type="text/javascript">
    <!--
    var areaname = "<?php echo $arename; ?>";
    
    if(areaname [COLOR="red"]==[/COLOR] "listings-residential"){
        document.location.href="residential.php";
    } else {
        document.location.href="commercial.php";
    }
    //-->
    </script>
    Code (markup):
    You need 2 equals signs otherwise your just setting areaname to the value on the right.

    = is set variable one to variable 2
    == is the comparision operator

    Check out w3schools.com for some great tutorials.
     
    Imozeb, Mar 27, 2010 IP
  3. JSellnau_TSCC

    JSellnau_TSCC Peon

    Messages:
    166
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I adjusted it and now it always go to the "else" link in the code. Hmmmmmmmm. I will have to check out those tutorials.
     
    JSellnau_TSCC, Mar 27, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Put single quote marks instead of double quotes around the string in the if statement.
     
    Imozeb, Mar 27, 2010 IP