spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column62


WMLScript Primer

Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

The Mortgage Example

In our previous column, we showed you the currency.wml and currency.wmls files. Let's examine another example provided by Nokia's WAP Toolkit, mortgage.wml and mortgage.wmls. Please refer to our previous column on how to download Nokia's WAP Toolkit and how to run WML decks with it. Here we show just the source code of mortgage.wml, mortgage.wmls, and the bytecode version of mortgage.wml. The size of mortgage.wml is 1071 bytes:

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="card1" title="Mortgage Calc" newcontext="true">
  <p>
      Principal: <input format="*N" name="principal"
       title="Principal:" value="150000"/>
      <br/>
      Interest:  <input format="*N" name="interest"
       title="Interest:" value="8.00"/>
      <br/>
      Num payments:<input format="*N" name="num_payments"
       title="Num. payments:" value="360"/>
      <br/>
      Payment = <u>$(payment)</u>
      <do type="accept" label="Calculate">
        <go href="mortgage.wmls#payment('payment','$(principal)',
         '$(interest)','$(num_payments)')"/>
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)
      </do>
      <do type="help" label="Help">
        <go href="#help"/>
      </do>
      </p>
  </card>
  <card id="help" title="Help">
    <p>
      <u>Principal</u> - $$ amount<br/>
      <u>Interest</u> - e.g. 7.5<br/>
      <u>Payments</u> - e.g. 360 for 30 years<br/>
      <do type="prev" label="Back">
        <prev/>
      </do>
      </p>
  </card>
</wml>

We have colored in red the call to the WMLScript function:


mortgage.wmls#payment('payment','$(principal)',
 '$(interest)','$(num_payments)')

The function payment() is defined in a different compilation unit, mortgage.wmls. The function accepts four parameters. The first one, payment, is the WML's variable which is to be updated by the WMLScript function. The definition of this variable in the caller WML code is colored in red above as well. The other three parameters, principal, interest, and num_payments, are passing values from the caller WML deck to the WMLScript function. Here is how this WMLS function is defined in mortgage.wmls:

/*
 * Calculate a mortgage's payment
 *
 *@param varname the variable name to store the result
 *@param principal the principal
 *@param interest the interest rate
 *@param num_payments the number of payments
 *@return the payment
 */
extern function payment(varname, principal, interest, num_payments) {
	/*
	 * Interest formulae:
	 *
	 * If (i != 0), then:
	 *  pmt = principal * [i * (1+i)^n / ((1+i)^n - 1)]
	 *
	 * If (i == 0), then:
	 *  pmt = principal / n
	 */
    var mi = interest/1200;
   // monthly interest from annual percentage
    var payment = 0;
	if (mi != 0) {
		var tmp = Float.pow((1 + mi), num_payments);
		payment = principal * (mi * tmp / (tmp - 1));
	} else {
		if (num_payments != 0)
			payment = principal / num_payments;
 	}
	var s;
	if (payment != 0)
		s = String.format("$%6.2f", payment);
	else
		s = "Missing data";

	/*
 	 * Send the result to the browser
	 */
    WMLBrowser.setVar(varname, s);

	/*
	 * Make sure the browser updates its current card
	 */
    WMLBrowser.refresh();
};

The computation algorithm of the mortgage payments is straight-forward and coded as someone would code it in JavaScript. Notice the extern declaration (colored in red) that is an extension to JavaScript. Also, notice the call to an external library, WMLBrowser. WMLScript provides several libraries. The advantage of libraries in the narrow-band wireless world is that they make the application code shorter, by providing a set of shareable functions. JavaScript does not support external libraries.

Next: How to read a bytecode

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel Article: Using Power & Display Context in the Intel Mobile Platform SDK
Internet.com eBook: Real Life Rails
IBM SCA Center Article: Simplifying Composite Applications with Service Component Architecture
Intel PDF: Quad-Core Impacts More Than the Data Center
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Intel PDF: Analysis of Early Testing of Intel vPro in Large IT Departments
Internet.com eBook: Best Practices for Developing a Web Site
Intel PDF: IT Agility through Automated, Policy-based Virtual Infrastructure
IBM CIO Whitepaper: The New Information Agenda. Do You Have One?
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
IBM Whitepaper: How are other CIOs driving growth?
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Actuate Download: Free Visual Report Development Tool
Red Gate Download: SQL Backup Pro
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
IBM SCA Download: Start Building SCA Applications Today
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags


Created: May 22, 2000
Revised: June 18, 2000

URL: http://www.webreference.com/js/column62/5.html