WMLScript can be loosely termed as a toned-down version of JavaScript. If JavaScript can do wonders with your Web pages, WMLScript is just as capable of adding that deft touch to your WML pages. Basically, WMLScript is the scripting language for wireless development. Let’s take a closer look at this standard.

Why is WMLScript necessary?
The three things I find most valuable about WMLScript are that it:

  1. Adds intelligence to what could be, otherwise, dull pages.
  2. Minimizes round trips to the server.
  3. Uses facilities provided by the underlying user-agent.

But as with JavaScript, WMLScript often ends up being used more for validating user input than anything else.

Here’s a rundown of WMLScript basics:

  • Variable declaration is compulsory in WMLScript, but initialization is not.
  • WMLScript is a weakly typed language, so it supports data types only internally. You do not have to specify the variable type, and any variable can contain any type of data at any given time.
  • In addition to the four standard data types (Boolean, integer, floating-point, and string), WMLScript supports a fifth data type, invalid, which is meant to be used when an invalid data type has to be distinguished from the other data types.
  • The extern keyword makes a function accessible to an outside file.

Give it a test drive
WMLScript is all about trying out things, so I would suggest that you visit the Nokia site for info or pick up Openwave’s WMLScript Language Reference and Guide and fiddle around with the multitude of features WMLScript has to offer.

Let’s look at some code snippets that could get you going. I’ve taken the example in my earlier WML article a little further in Listing A.

The href for the Submit button, instead of specifying a card name, specifies a WMLScript file, myscripts.wmls, and the function referred to is checkFunc. Listing B shows the WMLScript file.

The word extern is prefixed to function  checkFunc to ensure that the function is accessible outside myscripts.wmls. This function picks up the value of the variable OS, pops up a simple dialog, and then, based on the value of the variable, determines where to go next. #WINDOWS and #LINUX refer to the card IDs included in Listing A (DeckOne.wml). Listing C contains the WML for Decktwo.wml.

In this example, we get user input in the form of integers only. This is achieved by the format attribute. Be sure to check out the other values that the format attribute can take, as these can take care of a lot of the validations you would otherwise have to do in WMLScript.

When the user submits valid data, the weighcalc method is called. Here, we pick up the value of variable weight and ensure that it isn’t blank. The use of the parseInt method is not really required in this; however, I have used it to introduce you to the Lang library. This library has most of the methods you will need (such as min, max, parseFloat, abs, and random). Other libraries, like String, URL, Float, and Dialogs, can also do a lot of the mundane tasks for you. Make it a point to go through the methods these libraries provide so that you don’t end up reinventing the wheel.

Returning to our example, what comes next is some basic programming logic that is not at all language-specific. The variable remark is being set to a value based on the condition being fulfilled. Getting the hang of setting and getting variables in your WML and WMLScript is critical to truly exploit the power of scripts. In this case, the value of the variable remark is being set and the card display handles displaying the value of the variable.

Learn as you go
A great way to learn what tricks WMLScript can perform is to dig in and start hacking. You can take advantage of a variety of excellent Web sites offering WML and related content. Nokia’s site is one of the best.