Page 1 of 1

How to check if a variable is undefined?

Posted: Fri May 17, 2013 11:35 am
by w2gi
Hi,

In the following vxml, I am trying to find out if chosen.StreetPreDirectional variable has some value in it, before assigning.

Code: Select all

<if cond="chosen.StreetPreDirectional || chosen.StreetPreDirectional != undefined || chosen.StreetPreDirectional != 'undefined' || chosen.StreetPreDirectional != ''">
   <assign name="address" expr="address + ' ' + chosen.StreetPreDirectional" />
</if>
But, it always ends up concatenating "undefined" to the address variable.

Basically I want to know, how to check if a variable has a value. Or is it undefined or defined.

Thank you,
Mani

Re: How to check if a variable is undefined?

Posted: Fri May 17, 2013 1:00 pm
by support
Hi,

You can add Javascript to your code using the <script> tag and check if the varible is undefined by doing the following:

if(typeof variable_here === 'undefined'){ // your code here. };

The following example code demonstrates how to check if the street Predirectional is undefined or not:

Code: Select all

<?php
header("Content-type: text/xml");
echo("<?xml version=\"1.0\"?>\n");
?>
<vxml version="2.0">

<var name="mycity" expr="''"/>
<var name="mystate" expr="''"/>

<form id="citystate">
<field name="getcitystate" type="uscitystate">

  <prompt bargein="false">
    Say the city and state.
  </prompt>

  <filled>
    <prompt bargein="false">
      The city is <value expr="getcitystate.city"/>
      and the state is <value expr="getcitystate.state"/>
      in <value expr="getcitystate.county"/> county.
    </prompt>
    <assign name="mycity" expr="getcitystate.city"/>
    <assign name="mystate" expr="getcitystate.state"/>
    <goto next="#street"/>
  </filled>
</field>
</form>

<form id="street">
<field name="street">
  <grammar srcexpr="'http://vxml.plumgroup.com/grammars/usstreetaddress.php?city='+escape(mycity)+'&state='+escape(mystate)" root="usstreet" type="application/srgs+xml" mode="voice"/>

  <prompt bargein="false">
    Please say your full street address: the number first followed by the street 
    name and type.
  </prompt>

  <filled>
    <script> 
      if(typeof street.StreetPreDirectional === 'undefined'){ street.StreetPreDirectional='not given'; }; 
    </script>
    <prompt bargein="false">
      You said <value expr="street$.utterance"/>.
      The street number is <value expr="street.StreetNumber"/>.
      The street name is <value expr="street.StreetName"/>.
      The street suffix is <value expr="street.StreetSuffix"/>.
      The street pre-directional is <value expr="street.StreetPreDirectional"/>.
    </prompt>
    <goto next="#streetname"/>
  </filled>
</field>
</form>
</vxml>
Regards,
Plum Support

Re: How to check if a variable is undefined?

Posted: Fri May 17, 2013 4:01 pm
by w2gi
Thank you