We've Moved! Please visit our new and improved forum over at our new portal: https://portal.plumvoice.com/hc/en-us/community/topics

How to check if a variable is undefined?

Questions and answers about IVR programming for Plum DEV

Moderators: admin, support

Post Reply
w2gi
Posts: 46
Joined: Fri Jun 26, 2009 1:35 pm

How to check if a variable is undefined?

Post 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

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: How to check if a variable is undefined?

Post 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

w2gi
Posts: 46
Joined: Fri Jun 26, 2009 1:35 pm

Re: How to check if a variable is undefined?

Post by w2gi »

Thank you

Post Reply