Solved

modify this script

Posted on 2013-10-25
5
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
replace 'or' with the javascript operator '||'.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

10 Experts available now in Live!

Get 1:1 Help Now