XFA Specification
Chapter 9, Automation Objects
Document Variables
296
When a script document variable is instantiated, the contents of the element are compiled into a script
object and that object is then registered with the subform. From that point, when the document variable is
referenced the compiled script object is returned.
Declaring and Referencing Named Script Objects
Note:
Scripts can only reference script document variables if they have the same contentType.
Example 9.2
Declaring and using a named script object
<template>
<subform name="form1">
<variables>
<script name="foo" contentType="application/x-javascript">
var x = 0;
var y = 0;
var factor = 1;
function sum(val1,val2)
{
var sum = (val1 + val2) * this.factor;
return (sum);
}
</script>
</variables>
<field name="f1">
<!-- this example assumes naked references are available -->
<calculate>
<script contentType="application/x-javascript">
foo.x = 2;
foo.y = 2;
foo.factor = 2;
</script>
</calculate>
</field>
<field name="f2">
<!-- this example assumes naked references are available -->
<calculate>
<script contentType="application/x-javascript">
foo.sum(foo.x, foo.y);
</script>
</calculate>
</field>
<field name="f3"> <!-- this example doesn't use naked references -->
<calculate>
<script contentType="application/x-javascript">
xfa.form.form1.foo.x = 4;
xfa.form.form1.foo.y = 4;
xfa.form.form1.foo.factor = 4;
</script>
</calculate>
</field>
<field name="f4"> <!-- this example doesn't use naked references -->
<calculate>
<script contentType="application/x-javascript">
xfa.form.form1.foo.sum(xfa.form.form1.foo.x,