I am writting a large if / else tree in an existing website. My include libraries have to be put into the html program using a scripting language called SpeedScript. These includes bring in the login data that I am testing in my if tree. Instead of using the if/else I am trying a switch.

I need to know how to write this if/else tree / switch statement. I am brand new to Javascript. Here is the code I currently have that is not working.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>eSales Center - Customer Links</TITLE>
<META HTTP-EQUIV="Cache-Control" CONTENT="No-Cache">
<META HTTP-EQUIV="Pragma" CONTENT="No-Cache">
<META HTTP-EQUIV="Expires" CONTENT="0">


<SCRIPT LANGUAGE="SpeedScript">

{shared/esalesvars.i}
{shared/validate-session.i}
{shared/pp-global.i}

assign cLogin = if trim(cLogin) = "" then get-value("operinit") else cLogin.

</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="javascript">

switch (cLogin)
{
     case "12102":
          <a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />
          break
     case "134965":
          <a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />
          break
     case "13953":
          <a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />
          break
     case "13716":
          <a href="/WI_testweb/13716.xls"><b><font size="3">Matrix</font></b></a><br />
          break
     case "111667":
          <a href="/WI_testweb/111677.xls"><b><font size="3">Matrix</font></b></a><br />
          break
     default:
          window.alert("This account does not have a custom matrix");
 }
</SCRIPT>
</BODY>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>eSales Center - Customer Links</TITLE>
<META HTTP-EQUIV="Cache-Control" CONTENT="No-Cache">
<META HTTP-EQUIV="Pragma" CONTENT="No-Cache">
<META HTTP-EQUIV="Expires" CONTENT="0">


<SCRIPT LANGUAGE="SpeedScript">

{shared/esalesvars.i}
{shared/validate-session.i}
{shared/pp-global.i}

assign cLogin = if trim(cLogin) = "" then get-value("operinit") else cLogin.

</SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">

switch (cLogin)
{
	case "12102":
		document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
		break;

	case "134965":
		document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
		break;

	case "13953":
		document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
		break;

	case "13716":
		document.write('<a href="/WI_testweb/13716.xls"><b><font size="3">Matrix</font></b></a><br />');
		break;

	case "111667":
		document.write('<a href="/WI_testweb/111677.xls"><b><font size="3">Matrix</font></b></a><br />');
		break;

	default:
		window.alert("This account does not have a custom matrix");
}
</SCRIPT>
</BODY>
</HTML>

This did not work.

The only other change I can suggest is (based on my previous post), change: switch (cLogin) to switch (''+cLogin) to force cLogin into string context.

There are not syntax problems in the given javascript code, so if the problem persist, it must be related to SpeedScript. Are you sure you can "share/dereference" SpeedScript variables from javascript?

Thanks for all the help but so far no luck.

Well that came closer it fell through the switch to the default. I'm afraid the javascript isn't getting the variable from the speedscript at least not the data.

I guess Im wrong on the javascript not getting the data I just changed the window.alert to put out the ('cLogin") and it put the correct number in there now to get the case statements to see the number and react.

Ok I got the code to work with your help. THANK YOU here is the code that works

</SCRIPT>
</HEAD>
<BODY>

<SCRIPT type="text/javascript">
    window.onload = load;
    function load()
    {
        switch (`cLogin`)
        {
            case 12102:
                document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
                break;
            case 134965:
                document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
                break;
            case 13953:
                document.write('<a href="/WI_testweb/12102.xls"><b><font size="3">Matrix</font></b></a><br />');
                break;
            case 13716:
                document.write('<a href="/WI_testweb/13716.xls"><b><font size="3">Matrix</font></b></a><br />');
                break;
            case 111677:
                document.write('<a href="/WI_testweb/111677.xls"><b><font size="3">Matrix</font></b></a><br />');
                break;
            default:
                window.alert(`cLogin`);
        }
    }
</SCRIPT>
</BODY>
</HTML>

Like before, I was about to suggest

switch ('' + `cLogin`)

to force the value into string context and leave the quotes around the numbers in the case statements, but what you did is the inverse of what I was about to suggest. Glad it's working now.

Regards,
Hielo

PS: Don't forget to mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.