24
Welcome to Developer's Home Friday, February 11, 2011 WMLScript Introduction What is WMLScript? Preferences - Do not show ads WMLScript (Wireless Markup Language Script) is the client-side scripting language of WML (Wireless Markup Language). A scripting language is similar to a programming language, but is of lighter weight. With WMLScript, the wireless device can do some of the processing and computation. This reduces the number of requests and responses to/from the server. In the old days, fewer round-trips can improve the performance of your WAP site significantly since data transmission over wireless networks is slow. Today, the performance gained may not be so significant any more as data transmission speed has improved a lot. However, you may still find WMLScript useful since putting some operations at the client-side can reduce the load of your servers. WMLScript is based on ECMAScript (European Computer Manufacturers Association Script), which is JavaScript's standardized version. So, the syntax of WMLScript is very similar to JavaScript. (In case you do not know, JavaScript is a scripting language commonly used on the web.) If you have some programming experience with JavaScript, you

wapwml

Embed Size (px)

Citation preview

Page 1: wapwml

Welcome to Developer's Home Friday, February 11, 2011

WMLScript Introduction

What is WMLScript?

Preferences - Do not show ads

WMLScript (Wireless Markup Language Script) is the client-side scripting language of WML (Wireless Markup Language). A scripting language is similar to a programming language, but is of lighter weight. With WMLScript, the wireless device can do some of the processing and computation. This reduces the number of requests and responses to/from the server. In the old days, fewer round-trips can improve the performance of your WAP site significantly since data transmission over wireless networks is slow. Today, the performance gained may not be so significant any more as data transmission speed has improved a lot. However, you may still find WMLScript useful since putting some operations at the client-side can reduce the load of your servers.

WMLScript is based on ECMAScript (European Computer Manufacturers Association Script), which is JavaScript's standardized version. So, the syntax of WMLScript is very similar to JavaScript. (In case you do not know, JavaScript is a scripting language commonly used on the web.) If you have some programming experience with JavaScript, you should be able to learn WMLScript quickly. You may glance through or even skip some parts of this WMLScript tutorial.

A major difference between JavaScript and WMLScript is that JavaScript code can be embedded in the HTML markup, whereas WMLScript code is always placed in a file separated from the WML markup. URLs are used to refer to the actual WMLScript code in the WML document.

Page 2: wapwml

WMLScript has a number of standard libraries. They contain a lot of useful functions that you should get familiar with. We will talk about them in later parts of this WMLScript tutorial.

One common use of WMLScript is to validate form data. Another common use is to display message boxes to give alerts and error messages or to ask for confirmation of actions (no round-trip is needed for showing message boxes, which helps save bandwidth and improve the WAP application's response time).

WMLScript MIME Type and File Extension

WMLScript files have the extension ".wmls". The MIME type is "text/vnd.wap.wmlscript".

Page 1 of 71 Next Page

Table of Contents

Contents at a Glance

WMLScript Introduction

Hello World WMLScript Example

Compiling WMLScript Code

WMLScript Language Rules

Defining WMLScript Functions

Calling WMLScript Functions

WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Passing Arguments to Functions By Value and By Reference

Page 3: wapwml

WMLScript Operators

WMLScript Conditional Statements

WMLScript Looping Statements

WMLScript Standard Libraries Overview

WMLScript WMLBrowser Standard Library

WMLScript Dialogs Standard Library

WMLScript String Standard Library

WMLScript Float Standard Library

WMLScript Lang Standard Library

WMLScript URL Standard Library

WMLScript Example: Validating Form Data

Print this Web Page | Email Your Friends | Back to Top

Web DevelopersHome.com

Feedback Form (Expand)

What do you think about this web page?

It is very helpful.

It is helpful, but some information I wanted is missing.

It is not helpful.

It has broken links.

It has typos / grammatical mistakes.

Page 4: wapwml

It has incorrect information.

Others

WMLScript Tutorial Table of Contents

WMLScript Tutorial Contents at a Glance

Preferences - Change Color Scheme

Preferences - Do Not Show Ads

Next Page

Home | Acknowledgements | Advertise | Contact Us / Feedback | Glossary | Preferences

© 2004-2010 | Copyright Notice | Privacy Policy | Terms of Use

Welcome to Developer's Home Friday, February 11, 2011

Hello World WMLScript Example

Preferences - Do not show ads

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.

Page 5: wapwml

(helloWorldEg1.wml)

<?xml version="1.0"?>

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

<wml>

<card id="card1" title="WMLScript Tutorial">

<p>

<a href="helloWorldEg1.wmls#helloWorld()">Run WMLScript</a><br/>

$(message)

</p>

</card>

</wml>

Here is the file that contains the WMLScript code:

(helloWorldEg1.wmls)

extern function helloWorld()

Page 6: wapwml

{

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

WMLBrowser.refresh();

}

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

Page 7: wapwml

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:

Page 8: wapwml

WMLBrowser.refresh();

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#helloWorld()

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

Page 2 of 71 Previous Page | Next Page

Table of Contents

Contents at a Glance

WMLScript Introduction

Hello World WMLScript Example

Compiling WMLScript Code

Page 9: wapwml

WMLScript Language Rules

Defining WMLScript Functions

Calling WMLScript Functions

WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Passing Arguments to Functions By Value and By Reference

WMLScript Operators

WMLScript Conditional Statements

WMLScript Looping Statements

WMLScript Standard Libraries Overview

WMLScript WMLBrowser Standard Library

WMLScript Dialogs Standard Library

WMLScript String Standard Library

WMLScript Float Standard Library

WMLScript Lang Standard Library

WMLScript URL Standard Library

WMLScript Example: Validating Form Data

Print this Web Page | Email Your Friends | Back to Top

Web DevelopersHome.com

Feedback Form (Expand)

Page 10: wapwml

What do you think about this web page?

It is very helpful.

It is helpful, but some information I wanted is missing.

It is not helpful.

It has broken links.

It has typos / grammatical mistakes.

It has incorrect information.

Others

WMLScript Tutorial Table of Contents

WMLScript Tutorial Contents at a Glance

Preferences - Change Color Scheme

Preferences - Do Not Show Ads

Previous Page

Next Page

Home | Acknowledgements | Advertise | Contact Us / Feedback | Glossary | Preferences

© 2004-2010 | Copyright Notice | Privacy Policy | Terms of Use

Welcome to Developer's Home Friday, February 11, 2011

Page 11: wapwml

Compiling WMLScript Code

Preferences - Do not show ads

As you can see in the previous "Hello World" example, the WMLScript code is stored in plain-text form. Before the WMLScript file reaches the wireless device, it passes through a WAP gateway that compiles the WMLScript code. The byte code resulted is then transferred to the wireless device where it is executed.

Typically, there is no need to care about the WAP gateway part, as cellular network operators will provide a WAP gateway for their subscribers.

WMLScript Compilers

Although WAP gateways will take care of the compilation of WMLScript files for you, you can compile them manually if you want to (maybe for purposes such as checking syntax errors). All you need is a WMLScript compiler. One is available in the Nokia Mobile Internet Toolkit, which can be downloaded free of charge on Nokia's website.

To compile a WMLScript file using the Nokia Mobile Internet Toolkit, follow the steps below:

Run the Nokia Mobile Internet Toolkit

Click "File -> Open"

In the dialog box, choose the WMLScript file you want to compile and click the "Open" button

Page 12: wapwml

Click the "Compile" button in the bottom left-hand corner

If there is no compile-time error, the compiled file (ends with the extension ".wmlsc") will be saved and placed in the same directory as the original plain-text file.

Page 3 of 71 Previous Page | Next Page

Table of Contents

Contents at a Glance

WMLScript Introduction

Hello World WMLScript Example

Compiling WMLScript Code

WMLScript Language Rules

Defining WMLScript Functions

Calling WMLScript Functions

WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Passing Arguments to Functions By Value and By Reference

WMLScript Operators

WMLScript Conditional Statements

WMLScript Looping Statements

WMLScript Standard Libraries Overview

WMLScript WMLBrowser Standard Library

WMLScript Dialogs Standard Library

Page 13: wapwml

WMLScript String Standard Library

WMLScript Float Standard Library

WMLScript Lang Standard Library

WMLScript URL Standard Library

WMLScript Example: Validating Form Data

Print this Web Page | Email Your Friends | Back to Top

Web DevelopersHome.com

Feedback Form (Expand)

What do you think about this web page?

It is very helpful.

It is helpful, but some information I wanted is missing.

It is not helpful.

It has broken links.

It has typos / grammatical mistakes.

It has incorrect information.

Others

WMLScript Tutorial Table of Contents

WMLScript Tutorial Contents at a Glance

Page 14: wapwml

Preferences - Change Color Scheme

Preferences - Do Not Show Ads

Previous Page

Next Page

Home | Acknowledgements | Advertise | Contact Us / Feedback | Glossary | Preferences

© 2004-2010 | Copyright Notice | Privacy Policy | Terms of Use

Welcome to Developer's Home Friday, February 11, 2011

WMLScript Language Rules

Preferences - Do not show ads

WMLScript is developed based on ECMAScript, which is the standardized version of JavaScript. Thus, WMLScript's language rules are very similar to that of JavaScript.

Semicolons at the End of WMLScript Statements

A semicolon is required to end a statement in WMLScript. This is the same as C++ and Java. Note that JavaScript does not have such requirement. In JavaScript, semicolons are optional. The following code is taken from the earlier "Hello World" example. You can see that the two WMLScript statements in this function are ended with semicolons.

Page 15: wapwml

extern function helloWorld()

{

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

WMLBrowser.refresh();

}

Whitespaces in WMLScript

Except in string literals, WMLScript ignores extra whitespaces like spaces, tabs and newlines. Hence, the code in the earlier "Hello World" example can be typed in the following way and the result will remain the same:

extern function helloWorld()

{

WMLBrowser.setVar(

"message",

"Hello World. Welcome to our WMLScript tutorial.");

WMLBrowser.refresh();

}

Page 16: wapwml

Case Sensitivity in WMLScript

The WMLScript language is case-sensitive. For example, a WMLScript function with the name WMLScript_Function is different from wmlscript_function. So, be careful of the capitalization when defining or referring to a function or a variable in WMLScript.

Page 4 of 71 Previous Page | Next Page

Table of Contents

Contents at a Glance

WMLScript Introduction

Hello World WMLScript Example

Compiling WMLScript Code

WMLScript Language Rules

Defining WMLScript Functions

Calling WMLScript Functions

WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Passing Arguments to Functions By Value and By Reference

WMLScript Operators

WMLScript Conditional Statements

WMLScript Looping Statements

WMLScript Standard Libraries Overview

WMLScript WMLBrowser Standard Library

Page 17: wapwml

WMLScript Dialogs Standard Library

WMLScript String Standard Library

WMLScript Float Standard Library

WMLScript Lang Standard Library

WMLScript URL Standard Library

WMLScript Example: Validating Form Data

Print this Web Page | Email Your Friends | Back to Top

Web DevelopersHome.com

Feedback Form (Expand)

What do you think about this web page?

It is very helpful.

It is helpful, but some information I wanted is missing.

It is not helpful.

It has broken links.

It has typos / grammatical mistakes.

It has incorrect information.

Others

WMLScript Tutorial Table of Contents

Page 18: wapwml

WMLScript Tutorial Contents at a Glance

Preferences - Change Color Scheme

Preferences - Do Not Show Ads

Previous Page

Next Page

Home | Acknowledgements | Advertise | Contact Us / Feedback | Glossary | Preferences

© 2004-2010 | Copyright Notice | Privacy Policy | Terms of Use

Welcome to Developer's Home Friday, February 11, 2011

Comments in WMLScript

Preferences - Do not show ads

There are two types of comments in WMLScript: single-line comment and multi-line comment. To add a single-line comment, begin a line of text with the // characters. To add a multi-line comment, enclose the text within /* and */. These rules are the same in WMLScript, JavaScript, Java, and C++. The WMLScript engine will ignore all comments. The following WMLScript example demonstrates the use of comments:

extern function helloWorld()

Page 19: wapwml

{

// This is a single-line comment.

/* This is a

multi-line comment. */

/* A multi-line comment can be placed on a single line. */

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

WMLBrowser.refresh(); // A comment can be placed at a statement's end.

}

Page 5 of 71 Previous Page | Next Page

Table of Contents

Contents at a Glance

WMLScript Introduction

Hello World WMLScript Example

Compiling WMLScript Code

WMLScript Language Rules

Defining WMLScript Functions

Calling WMLScript Functions

WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Page 20: wapwml

Passing Arguments to Functions By Value and By Reference

WMLScript Operators

WMLScript Conditional Statements

WMLScript Looping Statements

WMLScript Standard Libraries Overview

WMLScript WMLBrowser Standard Library

WMLScript Dialogs Standard Library

WMLScript String Standard Library

WMLScript Float Standard Library

WMLScript Lang Standard Library

WMLScript URL Standard Library

WMLScript Example: Validating Form Data

Print this Web Page | Email Your Friends | Back to Top

Web DevelopersHome.com

Feedback Form (Expand)

What do you think about this web page?

It is very helpful.

It is helpful, but some information I wanted is missing.

It is not helpful.

It has broken links.

Page 21: wapwml

It has typos / grammatical mistakes.

It has incorrect information.

Others

WMLScript Tutorial Table of Contents

WMLScript Tutorial Contents at a Glance

Preferences - Change Color Scheme

Preferences - Do Not Show Ads

Previous Page

Next Page

Home | Acknowledgements | Advertise | Contact Us / Feedback | Glossary | Preferences

© 2004-2010 | Copyright Notice | Privacy Policy | Terms of Use