Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    North West
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    If Else Statement

    Code:
    <script type="text/javascript">
    var clicks=0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
        if (clicks>2) {
            var answer = confirm ("What choice would you like?")
                 if (answer)
                 window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no')
    	else (answer)
    	window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no')
            }
    }
    </script>
    OR

    Code:
    <script type="text/javascript">
    var clicks=0;
    function linkClick(){
       document.getElementById('clicked').value = ++clicks;
           if (clicks>2) {
               var answer = confirm ("What choice would you like?")
                  if (answer){
                     window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no')
    	         }
                  else (answer)
    	 window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no')
           }
    }
    </script>
    OK, so now I'm trying so open a specific page depending on the users answer. I am aware that my code should look like this:

    Code:
    if (condition)
      {
      code to be executed if condition is true
      }
    else
      {
      code to be executed if condition is not true
      }
    However because of my click counter I have been unable to do this.

    My code itself does kinda work BUT choiceA open both choiceA and choiceB's pages :-(

    choiceB is fine and opens choiceB's page.

    I know that it's probably a small syntax error but I just can't see where. Thank-you in advance for your assistance.
    Last edited by SpongeRob; 03-05-2012 at 01:49 PM. Reason: spelling mistake

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Although it's not mandatory to have {} for a branch of an if-else whene there is only 1 line in the branch but imo it is good practice to include them anyway because if you need to add extra lines in a branch and then forget to add the {}, the code will not work corectly.

    You have mis-matched brackets in your code.
    Last edited by webdev1958; 03-05-2012 at 11:41 AM.

  • Users who have thanked webdev1958 for this post:

    SpongeRob (03-05-2012)

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,502
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    var clicks=0;
    
    function linkClick(){
     document.getElementById('clicked').value = ++clicks;
     if (clicks>2) {
      var answer = confirm ("What choice would you like?");
      if (answer){
    //   window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no')
    alert("choiceA.html");
      }
      else {
    //   window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no')
    alert("choiceB.html");
      }
     }
     return false;
    }
    </script></head>
    
    <body>
    
    <a onmouseup="return linkClick();">LINK</a>
    <input id="clicked" />
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    SpongeRob (03-05-2012)

  • #4
    New Coder
    Join Date
    Mar 2012
    Location
    North West
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by webdev1958 View Post
    Although it's not mandatory to have {} for a branch of an if-else whene there is only 1 line in the branch but imo it is good practice to include them anyway because if you need to add extra lines in a branch and then forget to add the {}, the code will not work corectly.

    You have mis-matched brackets in your code.
    Thank-you for your explaination WebDev1958


    Quote Originally Posted by vwphillips View Post
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    var clicks=0;
    
    function linkClick(){
     document.getElementById('clicked').value = ++clicks;
     if (clicks>2) {
      var answer = confirm ("What choice would you like?");
      if (answer){
    //   window.open("choiceA.html",'','scrollbars=no,menubar=no,height=600,width=800,resizable=no,toolbar=no,location=no,status=no')
    alert("choiceA.html");
      }
      else {
    //   window.open("choiceB.html",'','scrollbars=no,menubar=no,height=450,width=450,resizable=no,toolbar=no,location=no,status=no')
    alert("choiceB.html");
      }
     }
     return false;
    }
    </script></head>
    
    <body>
    
    <a onmouseup="return linkClick();">LINK</a>
    <input id="clicked" />
    </body>
    
    </html>
    Thank-you very much VWPhillips, perfect!

    Thank's again guys :-)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •