This example demonstrates how to create a simple currency converter using WMLScript and WML. This example also demonstrates some of the ways in which WML and WMLScript interact.
The file currency.wml uses four cards to prompt the user for the input and output currency value and type and to present the results of the conversion. The file converter.wmls converts the input currency value to the specified output value and returns the results to the UP.Browser. A complete code listing follows:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.1//EN"
"http://www.phone.com/dtd/wml11.dtd">
<wml>
<card id="first" >
<onevent type="onenterforward">
<refresh>
<setvar name="startCur" value=""/>
<setvar name="endCur" value=""/>
<setvar name="cur" value=""/>
</refresh>
</onevent>
<p>
<do type="accept" label="conv">
<go href="#destVal"/>
</do>
Currency converter!
Convert from?
<select name="startCur">
<option value="USD">USD</option>
<option value="DMark">D-Mark</option>
<option value="Franc">Franc</option>
<option value="Lira">Lira</option>
</select>
</p>
</card>
<card id="destVal">
<p>
<do type="accept" label="dest">
<go href="#valCard"/>
</do>
Convert from $(startCur) to:
<select name="endCur">
<option value="USD">USD</option>
<option value="DMark">D-Mark</option>
<option value="Franc">Franc</option>
<option value="Lira">Lira</option>
</select>
</p>
</card>
<card id="valCard">
<onevent type="onenterforward">
<refresh>
<setvar name="cur" value=""/>
<setvar name="ans" value=""/>
</refresh>
</onevent>
<p>
<do type="accept" label="conv">
<go href="converter.wmls#convert()"/>
</do>
$(startCur)? (whole number)
<input type="text" name="cur" format="*N"/>
</p>
</card>
<card id="convD">
<p>
<do type="accept" label="new">
<go href="#first">
<setvar name="startCur" value=""/>
<setvar name="endCur" value=""/>
<setvar name="cur" value=""/>
</go>
</do>
<do type="options" label="again">
<go href="#valCard"/>
</do>
$(cur) in $(startCur) is $(ans) $(endCur)
</p>
</card>
</wml>
extern function convert(){
var scur = WMLBrowser.getVar("startCur");
var ecur = WMLBrowser.getVar("endCur");
var cur = WMLBrowser.getVar("cur");
var curVal = Lang.parseInt(cur);
if(curVal){
if(scur != ecur){
var endval = convertVal(scur, ecur, curVal);
}
else { endval = cur;}//no conversion
endval = String.format("%1.2f", endval);
WMLBrowser.setVar("ans", endval);
WMLBrowser.go("currency.wml#convD");
}
else{
error(cur);
}
}
function error(badVal){
Dialogs.alert("Your value " + badVal + " is bad");
WMLBrowser.setVar("cur", "");
WMLBrowser.go("#valCard");
}
function convertVal(scur, ecur, val){
// set the conversion values
var usd = 1;
var lira = 1858.22;
var dmark = 1.6535;
var franc = 5.5425;
// convert it all to usd
if (scur == "USD"){
} else if (scur == "Lira"){
val = val / lira;
} else if (scur == "DMark"){
val = val / dmark;
} else if (scur == "Franc"){
val = val / franc;
}
// convert to dest currency
if (ecur == "Lira"){ // going to Lira
return(val*lira);
}else if (ecur == "DMark"){ // going to Deutsch Mark
return(val*dmark);
}else if (ecur == "Franc"){ // going to French Franc
return(val*franc);
}else{ // going to US Dollar
return(val);
}
}
The initial card in currency.wml performs two tasks. First, it prompts the user for the currency to convert from, assigns the selected value to the variable startCur, and displays the next card in the sequence (destVal). Secondly, if the user arrived at the initial card from the last card (convD), the card initializes the startCur, endCur, and cur variables, preparing the currency converter for another cycle.
The next card in the sequence (destVal) prompts the user for the currency to convert to, assigns the selected value to the variable endCur, and displays the next card in the sequence (valCard).
The third card in the sequence (valCard) initializes the two variables cur and ans to empty strings, prompts the user for the numeric value in the currency to convert from, assigns the specified numeric value to the variable cur, and calls the external function convert in converter.wmls:
<go href="converter.wmls#convert()"/>
The last card in the sequence (convD) displays the results of the currency conversion:
$(cur) in $(startCur) is $(ans) $(endCur)
It also initializes the startCur, endCur, and cur variables and returns to the first card in the sequence if the user chooses to run a new conversion. This card also offers the user the opportunity to use the same source and destination currencies with a different numeric value to convert:
<do type="options" label="again">
<go href="#valCard"/>
</do>
The currency conversion takes place in converter.wmls, which then passes the converted values back to the UP.Browser so it can display them. This WMLScript declares one external function that serves as the entry point for the WML card, as described above:
extern function convert()
The convert function initializes its own variables to the values the user set using the WML cards with the WMLBrowser library function getVar:
var scur = WMLBrowser.getVar("startCur");
var ecur = WMLBrowser.getVar("endCur");
var cur = WMLBrowser.getVar("cur");
var curVal = Lang.parseInt(cur);
The last line in this block converts the string value derived from cur to an integer value that the internal function convertVal can use to make the conversion calculation.
The next section in convert tests curVal to make sure the user entered a valid value, if not, it calls the internal function error to inform the user that the conversion value entered is invalid and then prompts the user to enter a new value:
if(curVal){
...
}
else{
error(cur);
}
If curVal is a valid numeric value, convert then tests to make sure the user did not select the same currency type for both the input and output values. If so, no conversion is necessary and convert simply returns the input numeric value. If the user selected two different currency types, then a conversion is necessary. The internal function convertVal handles the conversion based on hard-coded exchange rates:
function convertVal(scur, ecur, val){
// set the conversion values
var usd = 1;
var lira = 1858.22;
var dmark = 1.6535;
var franc = 5.5425;
When convertVal returns the converted currency value, the convert function changes the integer value to a formatted string, returns it to the UP.Browser, and calls the last card in the WML sequence:
if(curVal){
if(scur != ecur){
var endval = convertVal(scur, ecur, curVal);
}
else { endval = cur;}//no conversion
endval = String.format("%1.2f", endval);
WMLBrowser.setVar("ans", endval);
WMLBrowser.go("currency.wml#convD");
}