Solved

modify this script

Posted on 2013-10-25
5
269 Views
Last Modified: 2013-10-25
<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
Comment
Question by:fedkris
  • 3
5 Comments
 
LVL 4

Expert Comment

by:smeghammer
ID: 39599801
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
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 39599804
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
 
LVL 4

Expert Comment

by:smeghammer
ID: 39599816
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
 

Author Comment

by:fedkris
ID: 39599824
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
 
LVL 4

Accepted Solution

by:
smeghammer earned 500 total points
ID: 39599835
replace 'or' with the javascript operator '||'.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now