Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2012, 11:31 AM   PM User | #1
SpongeRob
New Coder

 
Join Date: Mar 2012
Location: North West
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
SpongeRob is an unknown quantity at this point
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
SpongeRob is offline   Reply With Quote
Old 03-05-2012, 11:38 AM   PM User | #2
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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..
webdev1958 is offline   Reply With Quote
Users who have thanked webdev1958 for this post:
SpongeRob (03-05-2012)
Old 03-05-2012, 11:49 AM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
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.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Users who have thanked vwphillips for this post:
SpongeRob (03-05-2012)
Old 03-05-2012, 01:49 PM   PM User | #4
SpongeRob
New Coder

 
Join Date: Mar 2012
Location: North West
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
SpongeRob is an unknown quantity at this point
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 :-)
SpongeRob is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:02 AM.


Advertisement
Log in to turn off these ads.