WMLScript Tutorial

Preferences - Do not show ads

WMLScript (Wireless Markup Language Script) is the partner of WML. It is a client-side scripting language that is very similar to JavaScript. Our WMLScript tutorial begins with an overview of WMLScript and its language rules. Then we will look at functions and variables. You can learn how to define functions, how to declare variables, etc. Operators, conditional and looping statements will be covered afterwards. The WMLScript standard library contains a lot of useful functions. We will go through some of the commonly used ones. At the end of the WMLScript tutorial, we will demonstrate how to write scripts to validate forms and submit data to the WAP server.

Plenty of code examples are given in the WMLScript tutorial to help you understand the concepts and techniques.

Table of Contents

WMLScript Introduction

What is WMLScript?

WMLScript MIME Type and File Extension

Hello World WMLScript Example

Compiling WMLScript Code

WMLScript Compilers

WMLScript Language Rules

Semicolons at the End of WMLScript Statements

Whitespaces in WMLScript

Case Sensitivity in WMLScript

Comments in WMLScript

String Literals in WMLScript

Defining WMLScript Functions

The extern Keyword

WMLScript Function Naming Conventions

WMLScript Function Arguments

The return Statement

Calling WMLScript Functions

Calling a Function Located in the Same WMLScript File

Calling a Function Located in a Different WMLScript File

Calling a Function from a WML Card

Passing Arguments to Functions

WMLScript Variables

Declaring WMLScript Variables Using the var Keyword

WMLScript Variable Naming Conventions

Scope and Lifetime of WMLScript Variables

WMLScript Data Types

WMLScript Variables Vs WML Variables

Using WMLScript to Set the Value of a WML Variable

Using WMLScript to Obtain the Value of a WML Variable

Passing Arguments to Functions By Value and By Reference

WMLScript Operators

WMLScript Arithmetic Operators - Performing Math Operations

Basic Math Operations - Addition, Subtraction, Multiplication, Division, Finding Remainders

Pre/Post Increment and Decrement

Bitwise Operations - Dealing with Binary Representations of Numbers

WMLScript Assignment Operators - Assigning Values to Variables

WMLScript Logical Operators - Performing Boolean Operations

WMLScript Comparison Operators - Checking Two Values against a Certain Condition

WMLScript Conditional Operator - Choosing One Expression out of Two to Evaluate Depending on a Condition

WMLScript String Operators - Concatenating Strings

WMLScript typeof Operator - Finding Data Types of Variables

WMLScript isvalid Operator - Finding if a Variable is of the Invalid Data Type

WMLScript Conditional Statements

if Statement

WMLScript Looping Statements

while Statement

for Statement

break Statement: Quitting a Loop

continue Statement: Quitting Current Iteration of a Loop

WMLScript Standard Libraries Overview

How to Call a Function in the WMLScript Standard Libraries

WMLScript WMLBrowser Standard Library

Going Forwards / Backwards to WML Cards: go() and prev() Functions

Clearing Variables and URL History: newContext() Function

Getting the Current WML Card's URL: getCurrentCard() Function

Getting and Setting WML Variable Values: getVar() and setVar() Functions

Refreshing the Current Card on the WML Browser: refresh() Function

WMLScript Dialogs Standard Library

Displaying Messages on the Screen of Wireless Devices: alert() Function

Displaying Confirmation Messages on the Screen of Wireless Devices: confirm() Function

Displaying Input Fields and Getting Data from Users: prompt() Function

WMLScript String Standard Library

Getting the Character at a Certain Index in a String: charAt() Function

Getting the Number of Characters in a String: length() Function

Removing All Leading and Trailing Whitespaces of a String: trim() Function

Finding if a Certain Substring Exists in a String: find() function

Replacing a Substring with Another Substring: replace() Function

Manipulating Elements in Strings

Getting the Number of Elements in a String: elements() Function

Getting the Element at a Certain Index in a String: elementAt() Function

Inserting an Element at a Certain Position in a String: insertAt() Function

Removing the Element at a Certain Index in a String: removeAt() Function

Replacing an Element at a Certain Index with Another Element: replaceAt() Function

Arrays in WMLScript

Declaration of Arrays

Assigning Elements to an Array

Getting Elements of an Array

Changing the Value of an Array Element

WMLScript Float Standard Library

Rounding a Numeric Value: round() Function

Truncating the Fractional Part of a Numeric Value: int() Function

Raising a Numeric Value to the Power of Another Numeric Value: pow() Function

Calculating the Square Root of a Numeric Value: sqrt() Function

Calculating the Ceiling of a Numeric Value: ceil() Function

Calculating the Floor of a Numeric Value: floor() Function

WMLScript Lang Standard Library

Generating Random Numbers: seed() and random() Functions

Finding the Character Set Supported by the WMLScript Interpreter of a Mobile Device: characterSet() Function

Checking Whether Mobile Devices Support Floating-point Numbers and Operations: float() Function

Calculating the Absolute Value of a Numeric Value: abs() Function

Checking Whether the Conversion of a String into an Integer is Possible: isInt() Function

Converting a String to an Integer: parseInt() Function

Checking Whether the Conversion of a String into a Floating-point Number is Possible: isFloat() Function

Converting a String to a Floating-point Number: parseFloat() Function

WMLScript URL Standard Library

Getting the Current WMLScript Document's URL: getBase() Function

Escaping and Unescaping Special Characters in URLs: escapeString() and unescapeString() Functions

Extracting the Query String of an URL: getQuery() Function

Extracting Query Parameters of an URL

WMLScript Example: Validating Form Data

Retrieving Form Data and Removing Leading and Trailing Whitespaces

Checking Whether All Required Fields Has been Filled

Checking the Length of Password

Validating Email Address

Validating Date

Submitting Form Data to the WAP Server with WMLScript

At the Server-side