Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Couple things wrong with your use of <code>switch</code> and <code>case</code>:</p> <p><code>switch</code> is for evaluating a <em>single</em> expression and each <code>case</code> considered needs to be for the result of the expression analyzed.</p> <p>Since you're looking at two variables, <code>if</code>/<code>else if</code> would probably be cleaner:</p> <pre><code>if (type==1 &amp;&amp; height==1) { window.location = "http://www.maltabmx.com/About.html"; } else if (type==1 &amp;&amp; height==2) { window.location = "http://www.maltabmx.com/Footage.html"; } </code></pre> <p>That said, if you still want to use <code>switch</code>, you can, but you'll have to nest one within another. I don't recommend this as it's overly verbose but include an example here to help you understand your options.</p> <pre><code>switch (type) { case 1: switch (height) { case 1: window.location = "http://www.maltabmx.com/About.html"; break; case 2: window.location = "http://www.maltabmx.com/Footage.html"; break; } break; // other options... default: alert("You must select a type."); break; } </code></pre> <hr> <p>Added the following after reading the response comment:</p> <p>Since you have so many combinations, you could organize them using a combination of <code>if</code>/<code>else if</code>/<code>else</code> and <code>switch</code> statements. Since there are a large number of links, it's still going to be a chunk of code, but it would at least look a little cleaner.</p> <pre><code>var targetURL; if (type==0) { switch (height) { case 1: targetURL = "http://www.maltabmx.com/About.html"; case 2: targetURL = "http://www.maltabmx.com/Footage.html"; /* and so on... */ } } else if (type==1) { switch (height) { case 1: targetURL = "..."; case 2: targetURL = "..."; /* etc... */ } } else if (type==2) { switch (height) { case 1: targetURL = "..."; case 2: targetURL = "..."; /* etc... */ } } else { switch (height) { case 1: targetURL = "..."; case 2: targetURL = "..."; /* etc... */ } } targetURL ? window.location.href = targetURL : alert("Please choose a height."); </code></pre> <p>Since you mentioned you're still green to javascript, I'm going to go ahead and explain a couple other parts of my added example code. My apologies if I go over something you already know.</p> <p>From your HTML, it looks like type will always have a value, so rather than ending the <code>if</code>/<code>else if</code> chunk with <code>else if (type==3)</code>, I just use <code>else</code> since if type isn't 0, 1, or 2, it must be 3. It's generally good practice with <code>if</code>/<code>else (if)</code> statements to have a catch-all at the end, so your script always does something, even if the ending <code>else</code> is just to say something went wrong. That way the user doesn't sit there wondering if something's supposed to happen. You can do the same within a <code>switch</code> statement by specifying a <code>default</code> after your cases.</p> <p>I also used a variable called <code>targetURL</code> instead of using <code>window.location</code> on every line where <code>case</code> is used. When a variable is initialized without a value like I did ("<code>var targetURL;</code>"), the value becomes <code>false</code>. At the end of the script, I have some terse code that evaluates whether targetURL is true or not. If it's not true, it displays the alert to choose a height. In javascript, a variable will return as true as long as its value isn't <code>false</code> or <code>0</code>.</p> <p>(Please note that's boolean false and not the string "false"; <code>var targetURL = "false";</code> with the quotes will return true!)</p> <p>I did that so you don't have to type <code>window.location</code> 40 times and can put error handling (for if <code>height</code> somehow didn't have a value) in one place rather than four.</p> <p>And on that note, just to familiarize you with the terse syntax I just discussed:</p> <pre><code>targetURL ? window.location.href = targetURL : alert("Please choose a height."); </code></pre> <p>...is the equivalent of:</p> <pre><code>if (targetURL) { window.location.href = targetURL; } else { alert("Please choose a height."); } </code></pre> <p>You can make simple <code>if</code>/<code>else</code> evaluations more concise that way.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload