VoiceXML 2.1 Development GuideHome  |  Frameset Home


<script>  element

The script element is used for enclosing ECMAScript code to execute on the client side. Note that unlike the HTML element, the script element in voiceXML does not specify a type, as it must be ECMAScript only. Also note that either a src or inline content may be specified, but not both.


usage
<script charset="CDATA" fetchhint="(prefetch|safe|stream)" fetchtimeout="CDATA" maxage="CDATA" maxstale="CDATA" src="URI" srcexpr="CDATA">


attributes
charsetData Type: CDATADefault: Optional
The charset attribute defines the character encoding of the script contained within the element.
fetchhintData Type: (prefetch|safe|stream)Default: prefetch
Fetchhint is used to specify when the resource should be fetched during application execution. This element is currently ignored in the VWS 1.3+ voice browser. The possible values and their descriptions are:
  • prefetch : Begin the resource fetch upon initial document execution
  • safe: Only fetch the resource when it is specifically called in the application
  • stream:  Stream the content to the application as needed
It is also important to note that the value of stream has been phased out in VWS 2.0. Therefore it is recommended that you avoid using this value to ease future migrations to the newer platform.
fetchtimeoutData Type: CDATADefault: 5s
The fetchtimeout attribute allows the developer to specify how much time to allow on a fetch before throwing an error.badfetch event. This can be specified globally by using the fetchtimeout property in the application root document. If not specified, it will default to 5 seconds. Also note the strict formatting of this value; if specified, the time value must have the ‘s’ denomination appended to it, else an error.badfetch is thrown:

<script src="MyScript.js” fetchtimeout=”5s”/>
maxageData Type: CDATADefault: Optional
The maxage and maxstale attributes replace the VXML 1.0 caching attribute for compliance to the w3c vxml 2.0 specification. The value for this attribute specifies the maximum acceptable age, in seconds, of the resource in question. However, it is strongly advised not to rely on this attribute for cache-control; caching is always best controlled by the hosting server's response headers. If no headers are specified, then no cache control will be present, regardless of the value set for the maxage and maxstale attributes.
maxstaleData Type: CDATADefault: Optional
The maxage  and maxstale attributes replace the VXML 1.0 caching attribute for compliance to the w3c vxml 2.0 specification. The value for this attribute specifies the maximum acceptable staleness, in seconds, of the resource in question. However, it is strongly advised not to rely on this attribute for cache-control; caching is always best controlled by the hosting server's response headers. If no headers are specified, then no cache control will be present, regardless of the value set for the maxage and maxstale attributes.
srcData Type: URIDefault: Optional
The src attribute specifies the URI where the external script content is located. Note that either inline script content or an external src may be referenced, but not both.
srcexprData Type: CDATADefault: Optional
The srcexpr attribute evaluates to an ECMAScript value that defines the target URI. Either srcexpr or src may be specified for the element, but not both.



shadow variables
none


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


children
none


code samples
<MyScript.js>
function ruleFunction() {
return ("here is a nifty little sample script");
}


<Script maxage-maxstale> sample
<?xml version="1.0" encoding="UTF-8"?>

<vxml version = "2.1">

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>

<form id="F1">

<block>
  <script src="MyScript.js" maxage="5000"
maxstale="5000"/>

  <prompt><value expr="ruleFunction()"/> </prompt>
</block>

</form>

</vxml>

<Script fetchhint-fetchtimeout> sample
<?xml version="1.0" encoding="UTF-8"?>

<vxml version = "2.1">

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>

<form id="F1">

<block>
  <script src="MyScript.js" fetchhint="prefetch" fetchtimeout="10s"/>
  <prompt><value expr="ruleFunction()"/> </prompt>
</block>

</form>

</vxml>

<NBesting on Motorola>
<?xml version="1.0" encoding="UTF-8" ?>


<vxml version = "2.1">

<meta name="author" content="Matthew Henry"/>
<meta name="copyright" content="2005 voxeo corporation"/>
<meta name="maintainer" content="YOUR_EMAIL@HERE.COM"/>


<property name="maxnbest" value="5"/>
<property name="com.nuance.rec.DoNBest" value="1" />


<var name="resultLen"/>
<var name="resultArray"/>
<var name="myArray"/>

<form id="F1">

  <field name="F_1" slot="F_1">
    <prompt bargein="true">
      what is your favorite soft drink? I bet it is coca cola.
    </prompt>

    <property name="maxnbest" value="5"/>

    <grammar type="text/gsl">
      <![CDATA[
        [
        (koka cola)  { <F_1 "coke0"> }
        (coca cola)  { <F_1 "coke1"> }
        (koka colla) { <F_1 "coke2"> }
        (koka kolla) { <F_1 "coke3"> }
        (coka kola)  { <F_1 "coke4"> }
        ]
      ]]>




    </grammar>

    <catch event="nomatch noinput">Sorry, I didn't get that.
      <reprompt/>
    </catch>

    <filled>




      <assign name="resultArray" expr="application.lastresult$"/>
      <assign name="resultLen" expr="resultArray.length"/>
      <log expr="application.lastresult$"/>

    </filled>


  </field>

  <block name="B_1">
  <script>
      <![CDATA[
        function deleteElement(array, n) {
        var length = array.length;
        if (n >= length || n<0)                 
        return;

        for (var i=n; i<length-1; i++)     
        array[i] = array[i+1];             

        array.length--;
                           
                                    }
        deleteElement(resultArray, 0);
      ]]>
    </script>

  </block>

  <field name="Bool_0" type="boolean">
    <prompt>did you say \!sb <value expr="resultArray[0].utterance"/> \!se ? </prompt>

    <filled>
      <if cond ="resultArray.length == 1">
        <clear namelist ="Bool_0 F_1"/>
        <prompt>
          i am having trouble getting your response, mushmouth.
          lets start over, shall we?
        </prompt>
        <goto next="#F1"/>
      </if>


      <if cond="Bool_0==true">
        <prompt>
          excellent, everyone likes <value expr="resultArray[0].utterance"/>.
        </prompt>

      <else/>
        <clear namelist="Bool_0"/>
        <goto nextitem="B_1"/>
      </if>

    </filled>
  </field>
</form>

<form id="F2">

  <block>
    <prompt>thanks for taking the pepsi challenge, little buddy. </prompt>
    <goto next="#F1"/>
  </block>

</form>
</vxml>



additional links
W3C 2.0 Specification


  ANNOTATIONS: EXISTING POSTS
Lampei
9/27/2004 1:25 PM (EDT)
Is there a way to reference a script in another part of the same document, thus being able to reuse the same functions? e.g. If I put my script in the application file of my vxml project, can I then reference it something like application.form_name.block_name.my_function_here() ?
Lampei
9/27/2004 1:45 PM (EDT)
Also, w3.org has the script element available in both vxml and form elements.  Is this the same at Voxeo? (I only ask as they're not included in this "parent" list) http://www.w3.org/TR/voicexml20/#dml5.3.12  Thanks.
MattHenry
9/27/2004 4:08 PM (EDT)
Hi again,


I don't think that referencing a script like this is going to fly. If it was my own code, I would put all the client-side stuff in a seperate file, (myScript.js), and refernce it via the <script src> element/attribute.

This not only does the job, but keeps the JS segregated from the XML, making a much cleaner bit of code to view and edit.

And yes, I did forget to include the form and vxml elements as valid parent elements, which has been corrected. Thanks again for catching my goof.

~Matt
Asier
11/18/2005 6:11 AM (EST)
Hello there,

I need build this structure:

reg
  reg1
      ok
      pin
  reg2 (should be a dynamic array)
      cc
      saldo


and i dont know how do it, please help me to do it with any sample code, in ECMAScript.

Thanks in advance,
Asier
Asier
11/18/2005 8:21 AM (EST)
Sorry, this is possible like that:

function registro(ok, pin, numidem, nombre, idioma, sexo)
{
  this.reg1 = new registro1(ok, pin, numidem, nombre, idioma, sexo)
  this.reg2 = new Array;
}




function registro1(ok, pin, numidem, nombre, idioma, sexo)
{
  this.ok = ok
  this.pin = pin
  this.numidem = numidem
  this.nombre = nombre
  this.idioma = idioma
  this.sexo = sexo
}

function registro2(cc, saldo)
{
  this.cc = cc
  this.saldo = saldo
}


var reg = new registro("ok", "pin", "numidem", "nombre", "idioma", "sexo" ) ;
var regis2 = new registro2("cuenta corriente", "saldo");
var prueba = "pepe";
var n = 5;

for(i=0;i<n;i++)
{
var x = i;
var x = new registro2("cuenta corriente", "saldo");
reg.reg2.push(x);
reg.reg2[i].cc = "cuenta corriente " + i;
}


MattHenry
11/18/2005 10:31 AM (EST)

Hello Asier,

Glad you got it worked out.

~Matt 
jesusla
10/12/2006 4:15 PM (EDT)
Hi,

Do you have a list of all voxeo-specific global functions available within Javascript?

In particular, I'd like to know if there's a function to log to the voice browser log (akin to the "log" vxml element).

Thank you,
-Jesus
VoxeoTony
10/12/2006 6:28 PM (EDT)
Hello Jesus,

We do not have Voxeo-specific global functions available for Javascript.  We do support the log element based on the W3C spec, not a Voxeo-specific spec element. 

http://docs.voxeo.com/voicexml/2.0/log.htm

I hope this answer helps, please let us know if you need anything else.


Regards,

Tony
mho
10/19/2006 4:31 PM (EDT)
Note on external files (in case anyone else runs into this)
The system gets confused when you use things like %lt; and %gt; instead of < and > when you are using javascripts in extrnal files.
fliptone
5/1/2007 4:57 PM (EDT)
A client wants to enter an amount like DDDCC, where $154.12 would be entered as 15412. Of course they want it read back like a proper amount! I wrote a JavaScript function that builds an amount string from the digits, but I can't seem to figure out how to pass the field value to my function. I tried this:

<var name="formattedResponse" expr="formatResponse( response )" />

and

<assign name="formattedResponse" expr="formatResponse( response )" />

among other things but always seem to get back "$NAN". Help!

PS: They also want to enter dates MMDDYY so I have the same problem there as well!
MattHenry
5/1/2007 5:13 PM (EDT)


Hey folks,


I suspect that this is due to the fact that we are declaring the value as a string, instead of a number, and then trying to perform mathematical operations on it:

STRING:
<var name="myVar" expr="'1234'"/>

NUMBER:
<var name="myVar" expr="Number(1234);"/>

Give this a try, and see if you have better luck.

~Matt

fliptone
5/2/2007 10:05 AM (EDT)
Matt,

thanks for your help. (again!)

turns out I had my <assign> tag as the child of a <field> and so my JS function wasn't even running. Oddly, I wasn't getting a "bad document" error.

Thanks again.
MattHenry
5/2/2007 1:40 PM (EDT)


Hi there,


Bear in mind that as I don't have the full code that you are working with, I can only make guesses as to why this isn't working as intended. If you'd like to create a simplified test case, and attach it to this thread along with a thorough description of what isn't working for you, we might be able to offer you some accurate suggestions on how to fix the problem.


Regards,

~Matthew Henry
crishvi
2/5/2008 5:55 AM (EST)
hello!!i don't know where to post this so i'm posting it here, sorry if its not the right thread...i wanted to ask if there is any method to concatenate in vxml or even using script ??? I'm doing an application where the user should enter the password which is in characters...since it gets complicated through dtmf mode I'm planning to take the input from the user alphabet by alphabet and concatenate all those into a string through some loop function and get the password and read it out to the user. Please reply as soon as possible if there is some way out. thanks in advance.
mikethompson
2/5/2008 12:55 PM (EST)
Hello,

You can actually do string concatenation within the grammars themselves.  I'm not sure which style of grammar you're using (SRGS or GSL), but you should definitely check out the subgrammar sections for both languages:

http://docs.voxeo.com/voicexml/2.0/mot_appendixj.htm
http://docs.voxeo.com/voicexml/2.0/mot_appendixi.htm

In short, here's how you would string concat in GSL:

top level rulename
MYRULE [ 
NAME:d {<MyName $d>}    ]
; sub-rule
NAME    (
; assign results from 'FIRSTNAME' to slot 'a'
FIRSTNAME:a
; assign results from 'MIDDLENAME' to slot 'b'
MIDDLENAME:b
; assign results from 'LASTNAME' to slot 'c'
LASTNAME:c    )
; concatenate results from slots a, b, and c and return
{return(strcat($a strcat($b $c)))}

FIRSTNAME    [
[matthew] {return("matthew ")}
    ]

MIDDLENAME    [
[warren]  {return("warren ")}
    ]

LASTNAME    [
[henry]  {return("henry ")}
    ]

Hope this helps,
Mike Thompson
Voxeo Corporation
jefo12
6/20/2008 2:33 AM (EDT)
To request an appointment this week you can say today, tomorrow ,day after tomorrow or a specific day such as this Friday.you can also request the day next week such as next Friday.plz make ur request now Such as today,tomorrow,day after tomorrow,this Friday or next Friday. For a specific date, say date.

if the user says tuesday i hav to get the date as tuesday march 2nd
like that i hav to write the script canu plz suggest....
VoxeoDustin
6/20/2008 9:54 AM (EDT)
Hey Jefo,

That kind of script will require a bit of server side logic, as VXML does not inherently have that ability. I've included some links below on PHP that should get you pointed in the right direction on determining date with a little server side logic.

http://us.php.net/date
http://php.net/

Thanks,
Dustin
jefo12
6/24/2008 11:27 AM (EDT)
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1">



<script>

var myarr = new Array();


function ParseAppointments(str)

    {

    if(str.length==0)
return null;
else
      myarr=str.split("$");
         
    return myarr;
}
function giveLength(myarr)
    {
    return myarr.length;
    }
</script>

<form id="greeting">
<block>
<if cond="myarr=='null'">
<prompt> hai how are you </prompt>

      <else/>
<prompt>welcome back you have <value expr="giveLength(parseAppointments("10-08-2008$10:30"))"/>
      appointments </prompt>

<prompt> The appointments are </prompt>

<foreach item="applist" array="myarr">
            <prompt><value expr="applist"/><break/></prompt>
        </foreach>
</block>
</form>
</vxml>



this code is not working can you plz help me out to .....


what i'm having is i collect the appointments from the database in the form of strings and i pass it to a javascript and stores in an array..split the string and finally plays back all the  appointments....



voxeojohnq
6/24/2008 11:49 AM (EDT)
Jefo:

After briefly looking through your code, I noticed a few things that jumped out at me.

1) You should wrap everything inside of the <script> tags with <![[CDATA ]]> tags.  Although, this is not required for your sample code, i consider it part of best-coding practices.

2) <if cond="myarr=='null'">  this condition will never evaluate to true as myarr is an array or returned as null from your parseAppointments function.

3) You have " inside of the expr attribute in the <value> tag.  To clarify: <value expr="giveLength(parseAppointments("10-08-2008$10:30"))"/> is invalid because the value of expr containes "".  Try replacing the quotes with ''

4) This statement giveLength(parseAppointments("10-08-2008$10:30")) has a possibility of failing as the parseAppointments function will return null which is an invalid argument to giveLength.

Generally speaking, it is a good idea to open your XML document in FireFox or IE to check for parse errors.

Regards,

John Quinn
Voxeo Support

login



© 2008 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site