You are on page 1of 5

WMLScript Tutorial

• WMLScript is the scripting language used in WML pages.


• WML pages can be displayed in a WAP browser.
• WMLScript is used to validate user input, generate dialog boxes, view error messages, access
facilities of the user agent, and much more.

WMLScript Introduction
What you should already know
Before you continue you should have a basic understanding of the following:
• HTML
• JavaScript
• WML.

What is WML?
• WML stands for Wireless Markup Language. It is a mark-up language inherited from HTML, but
WML is based on XML, so it is much stricter than HTML.
• WML is used to create pages that can be displayed in a WAP browser.
• Pages in WML are called DECKS. Decks are constructed as a set of CARDS.

What is WMLScript?
• WMLScript is the scripting language used in WML pages
• WMLScript is a light version of the JavaScript language
• WML scripts are not embedded into WML pages
• WML pages contains references to script URLs
• WMLScript is compiled into byte code on the server before it is sent to the WAP browser
• WMLScript is a part of the WAP specification
• What is WMLScript used for?
• WMLScript is used to validate user input
• WMLScript is used to generate message boxes, and to view errors and confirmations faster
• WMLScript is used to access facilities of the user agent

WMLScript How to
• How to call a WMLScript from a WML page
• WMLScripts are not embedded in WML pages.
• A WML page only contains references to script URLs.

In the example below, if you select the go label, the external script will direct you to
http://www.w3schools.com/wap.wml:<?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="no1" title="Go to URL">
<do type="options" label="Go">
<go href="check.wmls#go_url('W3Schools')"/>
</do>
</card>
</wml>

The red line above contains a reference to a WMLScript. The script is in a file called check.wmls, and the
name of the function is go_url.

Here is the WML page called check.wmls:


extern function go_url(url)
{

1
if (url=="W3Schools")
{
WMLBrowser.go("http://www.w3schools.com/wap.wml")
}
}

Note that the function is using the extern keyword. When using this keyword the function can be called by
other functions or WML events outside the .wmls file. To keep a function private, drop the extern keyword.

WMLScript Dialogs Library


The Dialogs library contains functions that displays alert messages.
WMLScript Dialogs Library Functions
Function Description

alert() Displays a message and waits for a confirmation

confirm() Displays a message and waits for an answer

prompt() Displays a question and waits for an input

WMLScript Float Library


The Float library contains a set of math functions.

WMLScript Float Library Functions


The Float library works only on clients that support floating-point numbers. If floating-point numbers are not
supported, all functions should return invalid.
Function Description

ceil(x) Returns the nearest integer that is not smaller than a


specified number
floor(x) Returns the nearest integer that is not larger than a
specified number
int(x) Returns the integer part of a specified number
maxFloat() Returns the largest possible floating-point number
minFloat() Returns the smallest possible floating-point number
pow(x,y) Returns the value of x raised to the power of y
round(x) Rounds a number to the nearest integer
sqrt(x) Returns the square root of a number

WMLScript Lang Library


The Lang library contains functions for absolute value calculations, data type manipulation, and random
number generation.

WMLScript Lang Library Functions


Function Description

abort() Aborts a WMLScript and returns a message to the caller of


the script
abs(x) Returns the absolute value of a number

2
characterSet() Returns the character-set supported by the WMLScript
interpreter
exit() Exits a WMLScript and returns a message to the caller of
the script
float() Returns a Boolean value that indicates whether floating-
point numbers are supported
isFloat() Returns a Boolean value that indicates whether a value can
be converted into a floating-point number by the
parseFloat() function
isInt() Returns a Boolean value that indicates whether a value can
be converted into an integer by the parseInt() function
max(x,y) Returns the number with the highest value of x and y
maxInt() Returns the maximum possible integer value
min(x,y) Returns the number with the lowest value of x and y
minInt() Returns the minimum possible integer value
parseFloat() Returns a floating-point value defined by a string
parseInt() Returns an integer defined by a string
random(x) Returns a random integer between 0 and x
seed() Initializes the random number generator with a number,
and returns an empty string
Note: We think that the Lang library has a misleading name (should be named Math library or something like
that). However, it is called the Lang library because it contains functions that are closely related to the core
of the WMLScript engine.

WMLScript String Library


The String library contains functions for manipulating text.

WMLScript String Library Functions


Function Description

charAt() Returns the character at a specified position


compare() Compares two strings and returns a value that represents
the result of the comparison
elementAt() Divides a string into elements and returns a specified
element
elements() Returns the number of times a specified value appears in a
string
find() Returns the position of a substring in a string
format() Formats a value
insertAt() Divides a string into elements and inserts a substring at a
specified index position
isEmpty() Checks whether a string is empty

3
length() Returns the length of a string
removeAt() Divides a string into elements and removes a specified
element
replace() Replaces a part of a string with a new string
replaceAt() Divides a string into elements and replaces a specified
element
squeeze() Reduces all multiple spaces to single spaces in a string
subString() Returns a specified part of a string
toString() Converts a value to a string
trim() Returns a string without leading and trailing spaces

WMLScript URL Library


The URL library contains functions for handling URLs.

WMLScript URL Library Functions


Function Description

escapeString() Replaces special characters in a URL with an escape


sequence
getBase()
getFragment() Returns the fragment in a URL
getHost() Returns the host specified in a URL
getParameters() Returns the parameters in the last path segment of a URL
getPath() Returns the path specified in a URL
getPort() Returns the port number specified in a URL
getQuery() Returns the query part in a URL
getReferer()
getScheme() Returns the scheme in a URL
isValid() Returns true if a URL has the right syntax, and false if not
loadString() Returns the content and the content type of a specified
URL
resolve() Returns an absolute URL from a base URL and a relative
URL
unescapeString() Replaces the escape sequences in a URL with characters

WMLScript WMLBrowser Library

4
The WMLBrowser library contains functions that can be used to access browser variables.

WMLScript WMLBrowser Library Functions


Function Description

getCurrentCard() Returns the (relative) URL of the current card


getVar() Returns the value of a variable
go() Goes to a new card
newContext() Clears all variables
prev() Goes to the previous card
refresh() Refreshes the current card
setvar() Sets the value of a variable

Note: The WML specification states that calls to library functions that are not supported by the browser
should return invalid. Because of this, all the above functions should be tested against their return value,
and proper action should be taken in case a function returns invalid.

Source: http://w3schools.com/wmlscript/default.asp
By: x2d4

You might also like