Improve company productivity with a Business Account.Sign Up

x
?
Solved

modify this script

Posted on 2013-10-25
5
Medium Priority
?
282 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 2000 total points
ID: 39599835
replace 'or' with the javascript operator '||'.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

595 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