Creating and Reusing JavaScript Functions

The script object is an object you can use to store JavaScript functions and values separately from any particular form object. Typically, you use the script object to create custom functions and methods that you want to use as part of JavaScript scripts in many locations on your form. This technique reduces the overall amount of scripting required to perform repetitive actions.

The script object only supports script written in JavaScript; however, there are no restrictions on the location where the scripts are executed, provided that the scripting language for the event that invokes the script object is set to JavaScript.

Both Acrobat and Forms process scripting from a script object in the same manner, but both are also distinct.

Only scripts set to run on the client can make use of script objects set to run on the client, and vice versa.

To create a script object

There are two parts to creating a script object. The first part involves adding the object to the form design, and the second part is writing the script you want to store in the script object.

  1. Create a new form or open an existing form.

  2. In the Hierarchy palette, right-click either a form-level object or a subform-level object and select Insert Script Object.

    View full size graphic
    A. Form level object B. Subform level object C. Subform level script object D. Form level script object
  3. (Optional) Right-click the script object and select Rename Object.

To add script to a script object

After you have a script object on your form, you can add scripts using the Script Editor.

  1. Select the script object in the Hierarchy palette.

    The Script Editor is displayed with both a Script Object value in the Show list and a JavaScript value in the Language list. You cannot change either of these values.

  2. Enter your script in the Script Source field.

  3. Click the Preview PDF tab to test the form.

Example

For example, create a script object called feedback that contains the following function:

function emptyCheck(oField) { 
 
    if ((oField.rawValue == null) || (oField.rawValue == "")) { 
        xfa.host.messageBox("You must input a value for this field.", "Error Message", 3); 
    } 
}

To reference JavaScript functions stored in a script object

After you add scripts to a script object, you can reference the script object from any event that supports JavaScript scripts.

  1. Select an object on your form and select an event from the Show list.

  2. Create a reference to the script object and any functions within the script object. The following generic syntax assumes that the object where you are referencing the script object is at the same level as the script object in the form hierarchy or that the script object exists at the highest level of the form hierarchy.

    script_object . function_name ( parameter1 , ...);

  3. Apply the new script to the form object and test it by previewing the form using the Preview PDF tab.

Similar to referencing other objects on a form, you must provide a valid syntax when referencing the script object that includes where it exists within the form hierarchy. For more information about referencing objects in scripting, see Referencing object properties and values .

Example

For example, using the script object example from To add script to a script object , place the following JavaScript script on the exit event for a text field. Test the form using the Preview PDF tab.

// Ethnio survey code removed