Sunteți pe pagina 1din 7

WML SCRIPT

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.

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".
<?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>

WMLScript code: (helloWorldEg1.wmls)

extern function helloWorld()


{
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:

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.

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


{
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.

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:
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()

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();
}

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.

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


{
// This is a single-line comment.

/* This is a
multi-line comment. */

Defining WMLScript Functions

Functions
In WMLScript, all code must be encapsulated in functions. This is different from JavaScript in which a web developer
can choose whether to place the code in functions or directly in the markup. A function in WMLScript is defined
using the following format. The parts enclosed inside brackets [] are optional.

[extern] function function_name([argument1, argument2...])


{
WMLScript statements here
[return (some_value);]
}

The extern Keyword

The extern keyword is used to specify that a function can be called from both inside and outside of the WMLScript
file, i.e. the function can be called from functions in the same WMLScript file, from functions in a different
WMLScript file, or in a WML file. If you define a function without the extern keyword, the function can only be called
from functions in the same WMLScript file.

WMLScript Function Arguments

Arguments are used to pass values into a function. Unlike programming languages like C++ or Java, WMLScript does
not require you to specify the data type of an argument. For example, to pass two numbers into the WMLScript
function wmlscript_function(), you will define something like this:

function wmlscript_function(number1, number2)


{
...
}

If a function does not require any arguments, you still need to include the parentheses (), like this:

function wmlscript_function()
{
...
}

The return Statement

The "return (some_value);" statement is used to return a value back to the calling function in WMLScript. For
example, the calculateSum() function below returns the sum of two numbers back to the calling function each time it
is executed:

function calculateSum(number1, number2)


{
return (number1 + number2);
}

The wmlscript_function() function below calls calculateSum() with two arguments 1 and 2. The value 3 is returned
from calculateSum() and is assigned to the sum variable:
function wmlscript_function()
{
sum = calculateSum(1, 2);
}

It is not a must to include a return statement in a WMLScript function. If no return statement is included, the default
value, which is an empty string, will be returned.

Variable
A variable is used to store some data. You can modify or read the value of a variable during execution. The var
keyword is used to declare WMLScript variables. It should be used in the following form (the part enclosed within
brackets [] is optional):

var variable_name [= value_to_be_initialized];

Below is an example. The following line of code declares a variable called wmlscript_variable and initializes its value
to "Welcome to our WMLScript tutorial".

var wmlscript_variable = "Welcome to our WMLScript tutorial";

Variable initialization is optional. If you do not initialize a variable, the WMLScript interpreter will assign an empty
string to it automatically, i.e. the following line of script:

var wmlscript_variable;

is equivalent to:

var wmlscript_variable = "";

WMLScript does not support global variables. Global variables are variables that can be accessed from any functions.
This is different from JavaScript in which global variables are supported. For example, the following script is valid in
JavaScript but not in WMLScript:

var wmlscript_variable;

function wmlscript_func1()

{ wmlscript_variable = "Hello";

function wmlscript_func2()

{ wmlscript_variable = "Welcome to our WMLScript tutorial";

(Note: pls refer the book for further topics)

S-ar putea să vă placă și