Scripts
A <script> element executes an ECMAScript script, which is run in the scope of the parent element. A <script> element can also define functions that can be called by ECMAScript expressions in the same scope.
If your ECMAScript expression contains any of the characters “<“, “>”, or “&”, that character must be escaped. Inside a <script> element, you can do so in one of two ways. You can replace the individual characters with the corresponding escape sequence “<”, “>”, or “&”. Alternatively, you can place the entire script in a CDATA section. For example, either of the following is correct:
<script>
function factorial(n) {return (n <= 1) ? 1 : n * factorial(n-1); }
</script>
<script>
<![CDATA[
function factorial(n) {return (n <= 1) ? 1 : n * factorial(n-1); }
]]>
</script>
For a full discussion on ECMAScript in NVP, see ECMAScript reference .
ECMAScript variables
As previously mentioned, VoiceXML variables are equivalent to ECMAScript variables. Variables defined in VoiceXML can be used freely in a script as long as they are in scope, just as variables defined in a <script> element can be used freely in VoiceXML. Declaring a variable using a <var> element is equivalent to using a var statement in a <script> element.
Iteration
As part of your application, you may need to perform the same operation on all items in an ECMAScript array variable. You can do so using the <foreach> element, which executes content on all items in the specified array variable:
<var name="AvailablePizzaSizes"
expr="new Array('personal', 'small', 'large', 'extra-large')"/><prompt>Here's a list of available pizza sizes.</prompt>
<!-- iterate through each item in the array -->
<foreach item="psize" array="AvailablePizzaSizes">
<!-- Say the current array element -->
<prompt><value expr="psize"/></prompt>
</foreach>
The instructions within a <foreach> element may include any sort of executable content. See <foreach> for details.