• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

modify this script

<script>



			if ("<%GetField("F_MEMTYPE")%>"== "Associé") {
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			}
			
			else if ("<%GetField("F_MEMTYPE")%>"== "Provisoire"){
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			}
			
			else if ("<%GetField("F_MEMTYPE")%>"== "Affilié"){
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			}
			
			else if ("<%GetField("F_MEMTYPE")%>"== "Adhérent"){
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			
			}
			else if ("<%GetField("F_MEMTYPE")%>"== "Enseigne"){
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			
			else{
			
			document.write ("<table border='0' cellspacing='4' cellpadding='0' width='100%'>")


			} 

</script>

Open in new window


hey,

i want to modify the above code, (in asp it would be)


if associé, or provisoire, or affilié or adhérent or enseigne then

document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")

else

document.write ("<table border='0' cellspacing='4' cellpadding='0' width='100%'>")


end if


how can i do this, modify this script? (no asp script) thx
0
fedkris
Asked:
fedkris
  • 3
1 Solution
 
smeghammerCommented:
Hi,

Not sure what you mean - do you want a javascript equivalent structure? or do you want the ASP to write out the results of some ASP?

If javascript, use a switch/case structure:
var myvar = getRandomNumberBetweenZeroAndFive();
switch(myvar)
{
  case 0:
   //do something for zero
  break;
  case 1:
   //do something for one
  break;
  case 2:
   //do something for two
  break;
  case 3:
   //do something for three
  break;
  case 4:
   //do something for four
  break;
  case 5:
   //do something for five
  break;

  case default;
}

Open in new window


or to capture multiple cases:
var myvar = getRandomNumberBetweenZeroAndFive();
switch(myvar)
{
  case 0:
  case 1:
  case 2:
  case 3:
   //do something for zero, one, two or three
  break;
  case 4:
   //do something for four
  break;
  case 5:
   //do something for five
  break;

  case default;
}

Open in new window


For the latter, note the removal of the break statements, so that 0, 1,2 or 3 all drop through to the same handler block.

For your example, it would be:
//default output:
var htmlCode= "<table border='0' cellspacing='4' cellpadding='0' width='100%'>";
 
//either use client-side to get form field value, or a server var as you have done
var input = getFormField(); 
switch(input)
{
  case "associé":
  case "provisoire":
  case "affilié":
  case "adhérent":
  case "enseigne":
   htmlCode = "<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>"
  break;
  case default;
}

//and where you want the output:
document.write(htmlCode);  //or use DOM manipulation

Open in new window


I think switch/case is neater than multiple if/elseif statements (which you can of course do in javascript as well.)

An alternate way is to use an array and loop:
var stuffToCompare = new Array("associé","provisoire","affilié","adhérent","enseigne");
function compare(input)
{
  var output = "<table border='0' cellspacing='4' cellpadding='0' width='100%'>";
  for(var a=0;a<stuffToCompare.length;a++)
  {
    if(input == stuffToCompare[a])
    {
      //set output to alternate HTML if input matches an item in the array:
      output =  "<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>";
    }
  }
  return(output);
}

Open in new window

This is possibly a bit more involved, but will give you better control over the logic.
0
 
IronhoofsCommented:
I am not sure what you are asking (asp or not asp?). The script you allready have seems to do what you want, allthough clientside. If you want to do the check serverside and only send the appropiate <table> tag to the client, try to replace your code with this:

<%
switch (GetField("F_MEMTYPE")) {
  case "Associé":
  case "Provisoire":
  case "Affilié":
  case "Adhérent":
  case "Enseigne":
    Response.Write("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>\n");
    break;
	  
  default:
    Response.Write("<table border='0' cellspacing='4' cellpadding='0' width='100%'>\n")
    break;
}		
%>

Open in new window


N.B. the "<% %>"tags  are important because they tell the server that the content between those tags should be treated as executable ASP code.
0
 
smeghammerCommented:
Not if you use Ironhoofs option, don't forget to declare you ASP language as javascript at the top of your page:
<%@ language="javascript" %>

Open in new window

0
 
fedkrisAuthor Commented:
what's wrong in this script?

<script>

			if ("<%GetField("F_MEMTYPE")%>"== "Associé" or "<%GetField("F_MEMTYPE")%>"== "Provisoire" or "<%GetField("F_MEMTYPE")%>"== "Affilié" or "<%GetField("F_MEMTYPE")%>"== "Adhérent" or "<%GetField("F_MEMTYPE")%>"== "Enseigne") {
				document.write ("<table style='background-color:#C4F0B2;' border='0' cellspacing='4' cellpadding='0' width='100%'>")
			}
			
			else{
			
			document.write ("<table border='0' cellspacing='4' cellpadding='0' width='100%'>")


			} 

</script>

Open in new window

0
 
smeghammerCommented:
replace 'or' with the javascript operator '||'.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now