JavaScript

If / Then / Else

In the exeample given below, I needed to check two sets of values but they were related to tghe same set of settings. I test the value of AMremact is equal to "defaults", if so print out some text. Otherwise, I want to check if the value of AMremact is "Custom" and if so, what is the value of AMremactAA. Depending on that answer I write out a different set of values.

if ( document.SvrDeploy.AMremact.value == "defaults" ) { _ptxt.document.write("Use recommended defaults."); }
else if ( document.SvrDeploy.AMremact.value == "Custom") { 
	_ptxt.document.write("Use custom actions:");
	if ( document.SvrDeploy.AMremactAA.value == "ActiveAction") { _ptxt.document.write(" -- Use action recommended by ActiveAction."); }
	else if ( document.SvrDeploy.AMremactAA.value == "Custom") { _ptxt.document.write(" -- Use custom action: "+document.SvrDeploy.AMremactCust.value+"."); }
}

Switch statement

In the below example, I am testing for sveral different possible values that may be assigned to AMstat. In this Case if the value is either "Off" or "Inerited (Off)" then I amd setting a variable to to high. Note that I do this once. If I do not include the "break;" statement then the we just drop down through to the next and make the assignments given in the second test. After inhereited (high) I put a break statement at the end of the check. Also note at the end there is a check value for "default" so if the value check does not match any of the checks then the default value is assigned.
    // High
switch ( document.SvrDeploy.AMstat.value) {
	case "Off": 
	case "Inherited (Off)":
		SecPosture = "High"; 
		RecommendHigh.push("Trun on the Anti-Malware module.");
		break;
	case "On":
	case "Inherited (On)":
		SecPosture = "Low";
		break;
	default:
		SecPosture = "Medium";
		break;
}

Using Arrays

This is a rather long example of using an array. First I declare my arrays, RecommendHigh and RecommendMed. I step through some test statements such as if-then and switch-case statements. Here I use the ".push" method to append a new value to the tail-end of the array. This is a little different than most programming languarages where elements are pushed to the top of the stack. Finally, I output the contents of the array. In this case into an HTML unordered list (<UL> / <LI>). In this example I use the FOR/IN method but I could also use the ".length" property and use the traditional for(let i = 0; i < RecommendHigh.length; i++).

Though I did not test the ".forEach" property, I suspect that I could use that to produce the same output. It might look something like RecommendHigh.forEach(_ptxt.document.write("somevalue")); I must test that one at a later date and document it here. Additionally, I could use the ".pop" property to remove items from array and assign them to a variable for printing. In this web based example I thought that too inefficient.

// Define my arrays.
var RecommendHigh = [];
var RecommendMed = [];	

// Some test statements where I push data on the array.
// High
switch ( document.SvrDeploy.AMstat.value) {
case "Off": 
case "Inherited (Off)":
SecPosture = "High"; 
RecommendHigh.push("Trun on the Anti-Malware module.");
break;
}

if( document.SvrDeploy.DXP.checked == false ) { SecPosture = "High"; RecommendHigh.push ("Scanning all documents for known and unknown exploits."); }
if ( document.SvrDeploy.PMLenabled.checked == false ) { SecPosture = "High"; RecommendHigh.push ("Enabling Predictive Machine Learning (PML) and quarantining associated files.");}
if ( document.SvrDeploy.BMenabled.checked ==  false ) { SecPosture = "High"; RecommendHigh.push ("Enabling Behavior Monitoring (BM), quarantining associated files, and backup/restore ramsomeware encrypted files.");}
if ( document.SvrDeploy.AMSIenable.checked == false ) { SecPosture = "High"; RecommendHigh.push ("Enabling Anti-Malware Scan Interface (AMSI) protections and terminating associated processes.");}
if ( document.SvrDeploy.PMSenable.checked == false ) { SecPosture = "High";  RecommendHigh.push ("Enabling Process Memory Scanning for malware.");}
	
	//Write high risk items
if ( SecPosture == "High") {
	_ptxt.document.write("We strongly recommend the following settings:<ul>");
	for ( let config in RecommendHigh) { _ptxt.document.write("<li>"+RecommendHigh[config]+"</li>"); }
	_ptxt.document.write("</ul>");  
	}
		// Write medium risk items
	if ( SecPosture == "Medium") {
	_ptxt.document.write("We recommend the following settings:<ul>");
	for ( let config in RecommendMed) { _ptxt.document.write("<li>"+RecommendMed[config]+"</li>"); }
	_ptxt.document.write("</ul>");  
	}

For/In loop

Since we already used it above, I will just use the same example for the For/In loop but I will diagram it here. It looks like for ( let temp-var in array-var ). Obviously we start off with the "for" statement. We then declare a temporary vaiable. In the example below I called it "config". Next is the "in" operator followed by whatever it is operating on. In my case it is the "RecommendMed" array. Used with the document.write statements, this write out the values of the arry in an unorder list to the web page.

_ptxt.document.write("We recommend the following settings:<ul>");
for ( let config in RecommendMed) { _ptxt.document.write("<li>"+RecommendMed[config]+"</li>"); }
_ptxt.document.write("</ul>");  

Alert / Confirm

The Alert or Confirm command produces a dialogue box that the user must acknowledge before continuing. The focus is moved from the web page to the dialogue box and cannot be changed back without selecting the OK or CANCEL buttons depending on the method use. Text a veriable may be passed either the ALERT or CONFIRM cammand to display in the dialogue box.

<script>
function myInformation(myInfoTxt){
	confirm(myInfoTxt);
}
</script>

To get more information, click the button: <button onclick="myInformation('This is a test')">Info</button>
Back to Top

Email: Glen Geen