# <script>

Description

Includes a block of client-side ECMAScript. If a `<script>` occurs as a child of `<vxml>`, it is evaluated just after the document is loaded, in document order. If a `<script>` occurs as a child of a `<form>`, it is evaluated in document order each time execution moves into the `<form>` element. If a `<script>` occurs in executable content, it is evaluated as it is encountered. Each `<script>` element is executed in the scope of its containing element; i.e., it does not have its own scope. Variables defined in `<script>` are equivalent to variables defined using `<var>` within the same scope.

See the ECMAScript reference for details on both standard and VoiceGenie-defined features.

Syntax

    <script
        src="URI"
        srcexpr="ECMAScript_Expression"
        expr="ECMAScript_Expression"
        charset="encoding"
        fetchhint="prefetch" | "safe"
        fetchtimeout="time_interval"
        maxage="integer"
        maxstale="integer">
      script text
    </script>

Attributes

Attribute | Description
src | The URI specifying the location of the script. The URI can be one of the following formats: External script file: URL of the script file Local scripts: `builtin:[path/]filename` (a script named `/usr/local/phoneweb/script/[path/]filename` must exist on the IP Media Server platform) Optional. Exactly one of src, srcexpr, expr, or an inline script must be specified.
srcexpr | An ECMAScript expression evaluating to the URI of the script file, as documented under `src`, above. Optional. Exactly one of src, srcexpr, expr, or an inline script must be specified. This VoiceXML 2.1 feature will be ignored if the `<vxml>` `version` attribute is set to a value lower than 2.1 (i.e. 2.0 or 1.0).
expr | An ECMAScript expression evaluating to the URI of the script file, as documented under `src`, above. Optional. Exactly one of src, srcexpr, expr, or an inline script must be specified. This attribute was added as an extension to VoiceXML 2.0 in previous versions. It can only be used if the `<vxml>` `version` attribute is set to a value lower than 2.1 (i.e., 2.0 or 1.0). When the `<vxml>` `version` attribute is set to 2.1, the `srcexpr` attribute must be used instead of `expr`; using `expr` will cause an `error.badfetch` event to be thrown.
charset | The character encoding if an external script is used. Optional.
fetchhint | Defines when the script should be fetched. Optional. prefetch - script may be downloaded when the page is loaded safe - only load the script when needed
fetchtimeout | The length of time to wait for the script to be fetched before throwing an `error.badfetch` event. Optional.
maxage | Indicates that this document is willing to use a cached copy of the script only while the age of the cached copy is less than or equal to the number of seconds specified by this attribute. Optional.
maxstale | Indicates that this document is willing to use a cached copy of the script that has exceeded its expiration time by as much as the number of seconds specified by this attribute. Optional.

Attribute Notes

- `src`, `expr`, or `srcexpr` will be used even if they are empty strings, and an external script fetch will be attempted (the resulting absolute URI will likely be an invalid URI and this will result in an `error.badfetch` event being thrown).
- The Media Server always throws an `error.semantic` event when ECMAScript errors are encountered within a `<script>`.

Parents

<block>, <catch>, <error>, <filled>, <foreach>, <form>, <help>, <if>, <noinput>, <nomatch>, <vxml>

Children

#PCDATA

Extensions

- Added support for accessing builtin (local) script resources.
- Added `expr` attribute.

Limitations/Restrictions

- `<script>` cannot be used as a child of `<foreach>` when `<foreach>` is a child of `<prompt>`.

Example

It is wise to put CDATA escapes around your scripts so you don't have to escape XML reserved characters (eg. <, >, &, etc).

    <?xml version="1.0"?>
    <vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
      <form>
        <script>
          <![CDATA[
            var n = 5;
            function square(factor) {
              return factor*factor;
            }

        </script>
        <block>
          <value expr="n"/> squared is <value expr="square(n)"/>.
        </block>
      </form>
    </vxml>