<script>

Specifies a block of client-side scripting logic in JavaScript.

Syntax

 <script
     src="URI"
     charset="string"
     fetchhint="prefetch"|"safe"
     fetchtimeout="time_interval"
     maxage="time_interval" 
     maxstale="time_interval" 
     srcexpr="js_expression"
 >
   Script Text
 </script>

Description

Scripts do not have their own scope, but are executed in the scope of the containing element.

Attribute Description
src

URI of the script. Optional (as alternative to inline).

charset

Character encoding of the script if src is used. Optional.

fetchhint

Specifies whether the interpreter can attempt to optimize dialog interpretation by prefetching the resource. See Prefetching Resources. Optional.

fetchtimeout

Specifies the interval to wait for the resource to be returned before throwing a error.badfetch event. See Handling Fetching Delays. Optional.

maxage

New in VoiceXML 2.0. Specifies the maximum acceptable age, in seconds, of the cached resource. See Maximum Age. Optional.

maxstale

New in VoiceXML 2.0. Specifies the maximum acceptable time, in seconds, during which an expired cached resource can still be used. See Maximum Stale Time. Optional.

srcexpr

JavaScript expression that evaluates to grammar file URI. Optional (as alternative to src or an inline grammar).

If you specify this attribute, the element cannot have content.

(VoiceXML 1.0 only) The following attributes can be used in applications in which the version attribute of the <vxml> tag is set to 1.0.

Attribute Description
caching

VoiceXML 1.0 only. Specifies the caching policy for the resource being fetched. See Caching. Optional.

Used in place of the VoiceXML 2.0 attributes maxage and maxstale.

Tips:

 •  Inside a <script> element, if your JavaScript script contains any of the characters "<", ">", or "&", that character must be escaped. The easiest way to do this is to place the entire script inside a CDATA section, as in:
      <script>
         <![CDATA[
            function factorial(n) {
               return (n <= 1) ? 1 : n * factorial(n-1); }
         ]]>
      </script>

 

 •  Remember that any variables or functions declared in a script are valid only within the scope that contains the <script> element. Define the script in document scope (in the <vxml> element) if it defines items that you want to use in several dialogs or blocks. Any items defined by a script inside a block are accessible only within that block. In the following example, the first block contains a script that defines the function foo. That function can be used in a JavaScript expression later in the same block. However, it is illegal to use the function in a different block. The <value> tag in the second block will fail because the function foo goes out of scope when the interpreter leaves the first block.
      <block>
         <script>
            <!{CDATA[
               function foo() { return 1; }
            ]]>
         </script>
         <!-- This use of foo() is legal -->
         Foo is <value expr="foo()"/>
      </block>
      <block>
         <!-- ERROR!!!! This use of foo() is illegal -->
         Foo is <value expr="foo()"/>
      </block>

Usage

Parents Children
<bevocal:foreach>
<block>
<catch>
<error>
<filled>
<form>
<help>
<if>
<menu>
<noinput>
<nomatch>
<vxml>

None.

See Also

 •  VoiceXML 2.0 Specification: <script>
 •  JavaScript Quick Reference

Examples

Example 1--script to get the current time:

 <?xml version="1.0" ?>
 <!DOCTYPE vxml PUBLIC "-//BeVocal Inc//VoiceXML 2.0//EN"
  "http://cafe.bevocal.com/libraries/dtd/vxml2-0-bevocal.dtd">
 
 <vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
   <form id="form">
     <block name="time"> <!-- This block uses a java script to tell you the 
time-->
     
       <var name="hours"/>
       <var name="minutes"/>
       <var name="seconds"/>
       
       <script>
         var d=new Date();
         hours=d.getHours();
         minutes=d.getMinutes();
         seconds=d.getSeconds();
       </script>
       
       <prompt> 
         The time is now <value expr="hours"/> hours
         <value expr="minutes"/> minutes, and
         <value expr="seconds"/> seconds.
       </prompt>
     </block>
   </form>
 </vxml>

Example 2 script that defines a factorial function:

 <?xml version="1.0" ?>
 <!DOCTYPE vxml PUBLIC "-//BeVocal Inc//VoiceXML 2.0//EN"
  "http://cafe.bevocal.com/libraries/dtd/vxml2-0-bevocal.dtd">
 
 <vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
   <property name="universals" value="help" />
   <script> 
     <![CDATA[
       function factorial(n) { return (n <= 1) ? 1 : n * factorial(n-1); }
     ]]> 
   </script>
   <var name="max" expr="10"/>
   <form id="form-factorial">
     <field name="fact" slot="num" type="number">
       <prompt> 
         Tell me a number and I'll tell you its factorial.
       </prompt> 
       <catch event="help">
         <prompt>
            Please say a number more then zero and less than <value expr="max"/>
         </prompt>
       </catch>
       <filled>
         <if cond="fact &lt; max">
           <prompt>
             <value expr="fact" /> factorial is
             <value expr="factorial(fact)"/>
           </prompt>
         <else/>
           <prompt>
             Please choose a number less than <value expr="max"/>.
           </prompt>
           <clear namelist="fact" />
         </if>
       </filled>
     </field>
   </form>
 </vxml>

[Show Frames]   [FIRST] [PREVIOUS] [NEXT]
BeVocal, Inc. Café Home | Developer Agreement | Privacy Policy | Site Map | Terms & Conditions
Part No. 520-0001-02 | © 1999-2007, BeVocal, Inc. All rights reserved | 1.877.33.VOCAL