JavaScript Trick for Voice Applications

April 25, 2008


There are times when it is desirable to change the behavior of a VoiceXML application based on a specific setting.

For example, the GreenPhone application that I have mentioned in several previous posts has a setting that can be used to control whether special audio files are played. I personally find these audio files funny and somewhat endearing — others may not. To control whether they are played, there is a variable in the application root document called (cleverly) playAudio.

<br /> <var name="playAudio" expr="true"/><br />

It’s default setting is true, and this can be changed to false to prevent these files from playing. The typical method for checking a variable like this one to determine if an audio file should be played looks something like this:

`


  


`

There isn’t anything wrong with this, and since there isn’t a “cond” attribute on the tag there aren’t very many good alternatives. There is one alternative method that I rather fancy that uses the JavaScript conditional operator to distill this to a single line of code:

`