News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Does Java Script have a 'switch' statement

Started by Robert Collins, December 08, 2005, 06:53:50 PM

Previous topic - Next topic

Robert Collins

In my script function I am using if(k == 1){.....}; to direct control to the correct URL so I can display a new window for that URL.

This works fine but was wondering if Java script has a "switch" statement similar to 'C'? I would rather use a 'switch' instead of the 'if' but I have found no reference to it in any of my books on Java script.

Any such statement exist? Is there an alternative way instead of using 'if' statements?


<html>
<head>
<script>
function newWindow(k)
{
  var URL;

  if(k == 1){URL = "http://www.masmforum.com/simple/index.php"};
  if(k == 2){URL = "http://codeavenue.com"};

  window.open(URL);
}
</script>
</head>
<body topmargin="0" leftmargin="0" bgcolor="#FFFFFF">

<a href="javascript:newWindow(1)">Go to The MASM Forum</a><br>
<a href="javascript:newWindow(2)">Go to Code Avenue</a>

</body>
</html>

ramguru

According to "..javascript for dummies.." - yes, it has

switch
The switch statement provides an easy way to check an expression for a
bunch of different values without resorting to a string of if-else statements.
Here's the syntax:
switch (expression) {
case label :
statement
break
case label :
statement
break
...
default : statement
}

Robert Collins

Quote from: ramguru on December 08, 2005, 07:03:04 PM
According to "..javascript for dummies.." - yes, it has

switch
The switch statement provides an easy way to check an expression for a
bunch of different values without resorting to a string of if-else statements.
Here's the syntax:
switch (expression) {
case label :
statement
break
case label :
statement
break
...
default : statement
}


I tried this:


  switch(k)
  {
     case 1: URL = "http://www.masmforum.com/simple/index.php";
     case 2: URL = "http://codeavenue.com"};
  }


...but it didn't work. I think now by looking at your example I forgot the 'break' statement because it always went to the  2nd case statement.

Anyway, I have decided not to use either; instead I will pass the URL to the function


<html>
<head>
<script>
function newWindow(URL)
{
  window.open(URL);
}
</script>
</head>
<body topmargin="0" leftmargin="0" bgcolor="#FFFFFF">

<a href="javascript:newWindow('http://www.masmforum.com/simple/index.php')">Go to The MASM Forum</a><br>
<a href="javascript:newWindow('http://codeavenue.com')">Go to Code Avenue</a>

</body>
</html>


Thanks anyway.

ramguru

Try this, man: without semicolons and with one break

  switch(k)
  {
     case 1: URL = "http://www.masmforum.com/simple/index.php"
        break
     case 2: URL = "http://codeavenue.com"
  }

Ghirai

MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

Robert Collins

Quote from: Ghirai on December 09, 2005, 01:22:37 AM
Use Perl :8)

Perl? The scripts must run in a Web Page. Correct me if I am wrong but Perl is not supported as a Web Page script language. It's used for backend processing.