# Adding an ECMAScript Object file

An ECMAScript Object file is an external ECMAScript file that is called using the < script> tag in VoiceXML. This file contains one defined function with numerous defined properties. The properties represent subdialog-specific parameters, subdialog-specific prompts, and self-revealing help.

This page includes examples of how to add ECMAScript in VoiceXML files. Although the ECMAScript code could be embedded in a subdialog, it has been separated out to make it easier for you to use and to translate, if desired.

- VoiceXML variables are completely equivalent to ECMAScript variables.
- ECMAScript objects are used as the parameter passing mechanism for elements such as <subdialog> and <object>.
- The "expr" attribute available with many tags defines an ECMAScript expression.
- ECMAScript can be placed in-line in the VoiceXML document using the < script> element; scripts can also be loaded from a URI.
- Variable scoping (mostly) follows the ECMAScript model (session -> application -> document -> dialog -> anonymous).

One of the benefits of ECMAScript is that you can access VoiceXML variables within ECMAScript. Elements that accept the "expr" attribute can use arbitrary ECMAScript code to generate a value at runtime. And you can abstract your commonly used ECMAScript functions into functions or libraries to support reuse in your VoiceXML pages.

This file provides simple examples of how to:

- Add an ECMAScript Object file
- Code ECMAScript inline

#### Code ECMAScript inline

The following snippet demonstrates how to code ECMAScript inline using the VoiceXML editor. The ECMAScript function is included in the CDATA block. This example is of a toy store that is giving everything away free except for dolls. The ECMAScript finds the total cost by multiplying the number of dolls desired by the cost of a single doll.

    ...
    <script>
      <![CDATA[
        function totalCost(dolls) {
          return ( dialog.dolls_cost * dolls);
        }
      ]]>
    </script>

    <form id="tag_sale" scope="dialog">
      <var name="dolls_cost" expr="8.50"/>
      <block>
        <if cond="toyItem == 'dolls'">
          <prompt version="1.0">
            Your
            <value expr="toyItem"/>
            will cost
            <say-as interpret-as="vxml:currency">
            <value expr="totalCost(itemCount)"/></say-as>
          </prompt>
          <else/>
          <prompt version="1.0">
            You can have the
            <value expr="toyItem"/>
            for free.
          </prompt>
        </if>
      </block>
    </form>
    ...

The example calls the ECMAScript function from the VoiceXML code using the expr="totalCost(itemCount)" attribute. Then it passes the ECMAScript variable "itemCount," which was assigned a value in an earlier form (not shown in this example), as a parameter to the function. The ECMAScript function refers to the dialog-scoped variable "dolls_cost" that is declared and assigned a value within the form, which can then be accessed anywhere in the dialog.

Alternatively, you could declare the dolls_cost variable and assign it a value inside the <script> tags.

Related Information
What is VoiceXML?
What is ECMAScript?
Adding VoiceXML elements and attributes
Adding a grammar
Adding DTMF input
Ecma International (Standards)