The Company
Products
Solutions
Services and Support
Customers
Partners
News
Events
Home >> News >> WebFOCUS Newsletter >> September 2005 >> Maintain and JavaScript: Passing Parameters

Maintain and JavaScript: Passing Parameters

By Mark Derwin

JavaScript is a powerful tool programmers can use to manipulate objects on a Maintain Winform. You can use JavaScript to display messages and warnings, change the color and look of an object on a form, and validate data entered onto a form. One thing that JavaScript cannot do is read the value from a Maintain stack or variable. But don't worry, I am going to show you a technique to get around this limitation.

In order for JavaScript to be able to see the value from a stack or variable, you have to place it on a form. It is important that you look at the exact name of the object. If you have a field name called Emp_name, and you drag it onto a form, Maintain gives it the name Emp_name_Edit. Edit is added to the name of the editable field. Text is added to the name of the heading. Of course you can rename the field on the Properties sheet. Now we can create the JavaScript to get the value from the field. Create an event for an object, such as a button. Make sure you click the J at the top of the event editor. This tells Maintain the event is JavaScript. Instead of Case, the word function is displayed:

Function OnButton1_Click( ) {
var1 = Form1.Emp_name_Edit.value;
alert(var1);
}

Here, Form1 is the name of my form. When Button1 is clicked, JavaScript gets the data in the Emp_name_Edit field and alerts it back to the user. We could also use this technique to populate another field on the form. If I have a field on the form named Employee, I can have the following:

Function OnButton1_Click( ) {
var1 = Form1.Emp_name_Edit.value;
Form1.Employee_Edit.value = var1;
}

In addition to reading values on the Winform, JavaScript can set them as well. A similar technique can be employed to make sure the user has entered data into a field. In the example, we make sure that the length of the data is not zero. If it is, alert an error to the user:

Function OnButton1_Click( ) {
var1 = Form1.Emp_name_Edit.value.length;
if (var1 == 0)
{
alert("Enter a value");
}
}

As mentioned previously, for JavaScript to see Maintain values, the fields must be placed on the Winform. However, there will be times when you don't want the user to see those fields. In that case, edit the field's properties and set Visible to 0 - No. The field will not be visible to the user, but JavaScript can still see it. If the field has a heading, remember to set the properties for field_Text to Visible 0 - No as well. Since Maintain Winforms are effectively HTML at runtime, almost anything that you can do with JavaScript works. These techniques allow you great flexibility with Winform manipulation and validation.