2. Hello World WMLScript Example

An effective way to learn a new language is to go through examples. The following "Hello World" WMLScript example shows you how a WMLScript file typically looks like and demonstrates how to call WMLScript code in a WML document.


<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN" "http://www.wapforum.org/DTD/wml13.dtd">

  <card id="card1" title="WMLScript Tutorial">
      <a href="helloWorldEg1.wmls#helloWorld()">Run WMLScript</a><br/>

Here is the file that contains the WMLScript code:


extern function helloWorld()
  WMLBrowser.setVar("message", "Hello World. Welcome to our WMLScript tutorial.");

Open the helloWorldEg1.wml file in a mobile phone browser and you can see something like this:

Sony Ericsson T68i

Nokia Mobile Browser 4.0

If you select the "Run WMLScript" link, the WMLScript function helloWorld() is executed and the line "Hello World. Welcome to our WMLScript tutorial." will appear in the mobile phone browser.

Sony Ericsson T68i

Nokia Mobile Browser 4.0

In the above example, the WMLScript code is not embedded in the WML markup and they are kept in separate files. This is the rule of WMLScript and you need to follow this when programming mobile Internet browsing applications.

There is only one function, helloWorld(), in the WMLScript file. The extern keyword is used to specify that the helloWorld() function is allowed to be called from outside the WMLScript file helloWorldEg1.wmls. The extern keyword is necessary here since we want to call the function from the WML file helloWorldEg1.wml.

Inside the helloWorld() function, we use two functions of the WMLBrowser standard library, setVar() and refresh(). The setVar() function is used to assign a value to a WML variable. We use the WMLScript code:

WMLBrowser.setVar("message", "Hello World. Welcome to our WMLScript tutorial.");

to assign the value "Hello World. Welcome to our WMLScript tutorial." to a WML variable named message.

The refresh() function is used to instruct the WAP browser to refresh the current WML card. In the helloWorld() function, after we have assigned a value to the message variable, we make use of the line:


to refresh the WML card so that the change made to the message variable is shown on the screen of the mobile device.

To call the WMLScript function helloWorld() in the WML document, we use the URL below:


helloWorldEg1.wmls is the file that contains the WMLScript code and helloWorld() is the function to call.

Previous Page Page 2 of 71 Next Page

Feedback Form (ExpandCollapse)

What do you think about this web page?

(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page