Enforcing strict scoping rules in JavaScript

When working with JavaScript in forms, it is important to declare objects and variables within the scope they are intended. Globally declaring objects or variables unnecessarily can cause performance problems.

Strict scoping was introduced in Designer 8.1 to improve the run time and memory usage of a form. Strict scoping is enabled, by default, in Designer 10, for new forms. For old forms the option to enable strict scoping is available.

What is scope in JavaScript?

Scope works outwardly so that everything within curly brackets ({}) can see outside them. However anything outside the curly brackets cannot access anything inside them.

In the following example, the first curly bracket opens the function scope and the second one closes it. Everything between the curly brackets is within the scope of foo ().

The scope in the following example is valid because var nFooVar = nOutsideVar inside the curly brackets can see var nOutsideVar = 2 outside the curly brackets.

In contrast, the following example shows an invalid scope because var nOutsideVar = nFooVar cannot access var nFooVar =4 within the curly brackets.

Scope in scripting describes pieces of scripts that can access pieces. TShe pieces of script can be variables or functions.

What is scope XML?

Scope in a form design is about hierarchy. For example, to access the subform inside in the following XML source, you must type outside.inside.

<subform name="outside"> 
    <subform name="inside"> 
    … 
    </subform> 
</subform>

You do not type inside.outside because you must access the outermost subform first and drill inwards.

SOM expressions and scope

In forms that are targeted for Acrobat or Adobe Reader 8.1, SOM expressions are properly scoped as shown in this example:

    <subform name="a"> 
 
        <subform name="b"/>

In forms targeted for Acrobat or Adobe Reader 8.0, the SOM expression a.b.a returns the subform a. In forms targeted for Acrobat or Adobe Reader 8.1, the SOM expression a.b.a returns null because subform b does not have a child named a. In Acrobat or Adobe Reader 9.0 or later, the expression returns an error because a is not a valid child of b.

In Acrobat or Adobe Reader 8.1, functions and variable within a node’s script do not become global (script objects being the exception) as shown in this example:

    <field name="field1"> 
    event activity="initialize"> 
 
            <script contentType="application/x-javascript"> 
 
            // Function bar() is scoped to field1.initialize; nothing outside <event activity="initialize"> scope can see inside here (in 8.1) 
 
            function bar() 
 
            { 
 
                return "bar"; 
 
            } 
 
        </script> 
 
    </event> 
 
    /field> 
 
    field name="field2"> 
 
        <event activity="click"> 
 
            <script contentType="application/x-javascript"> 
 
                field1.bar(); 
 
            </script> 
 
        </event> 
 
    </field> 

When you click field 2 in a form targeting Acrobat or Adobe Reader 8.0, the function bar() executes.

When you click field 2 in a form targeting Acrobat or Adobe Reader 8.1, the function bar() does not execute. The reason is because function bar() is available only from within the initialized script of field1.

Scoping and script objects

Script objects have global scope; therefore, anyone can access them from anywhere. If you have a method that you want both field1.initialize and field2.click to access, place it in a script object. Strict scoping means that you cannot call bar() from anywhere in a form. You also get a run-time error indicating that the function bar() could not be resolved. The script engine looked for bar() within the scope that you have access to and did not find it.

Scoping and target version

If you use strict scoping, remember that you get performance improvements in forms targeted for Acrobat or Adobe Reader 8.1 and later. Avoid using strict scoping in forms targeted for older versions of Acrobat or Adobe Reader. Otherwise, the scripts in the forms can work differently. For existing forms, back up the form before you enable strict scoping and always verify the script afterwards. When you enable strict scoping and then change the target version to earlier than Acrobat or Adobe Reader 8.1, warning messages appear.

When to use scoping

When a form is targeted for Acrobat or Adobe Reader 8.1 and strict scoping is on, declared JavaScript variables are released after each script executes. When a form is targeted for Acrobat or Adobe Reader 9.0 and later, strict scoping does not release all the JavaScript variables. The exception is when you remerge or import new data.

The performance enhancements with strict scoping rules apply to forms targeted for Acrobat or Adobe Reader 8.1 and later. Do not apply strict scoping rules to forms that are targeted for versions of Acrobat or Adobe Reader earlier than 8. Otherwise, the scripts can behave differently or not work.

To enable strict scoping

  1. Select File > Form Properties and click the Run-time tab.

  2. Select Enforce Strict Scoping Rules In JavaScript , if the option is available, and then click OK.

    Note: If the option to enforce strict scoping rules is not available in the Run-time tab, then strict scoping is already enabled.

// Ethnio survey code removed