...

View Full Version : Resolved If Else Statement



SpongeRob
03-05-2012, 12:31 PM
<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



<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:



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.

webdev1958
03-05-2012, 12:38 PM
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.

vwphillips
03-05-2012, 12:49 PM
<!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>

SpongeRob
03-05-2012, 02:49 PM
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




<!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 :-)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum