Standard
Libraries
WMLScript contains several standard libraries with functions that
can be called by other functions in their own compilation units. Libraries are
collections of functions that are logically grouped together based on what the
library functions do. The functions are called by using a dot separator, as has
been shown in examples in the last two chapters. This is similar to other
object-oriented languages with the library name to the left of the dot, and the
called function and its parameters to the right.
These libraries are stored in the WAP browser, which is resident
in the WAP-compliant device. There are currently eight libraries available to
use in WMLScript: Lang, Float, String, URL, WMLBrowser, Dialogs, Debug, and Console.
Example 6.2 shows a
WMLScript function, which uses the length function from the String library, and the setVar and refresh functions from the WMLBrowser library. The length function returns the length of the string, and the setVar function sets the value of a variable, and refresh updates the WML document in the browser.
Figure 6.2 shows
the associated screens.
Appendix II lists all of the functions in each of the
WMLScript libraries. The following sections will describe the purpose of each
library, and give an example of one or more of the functions in that library.
But, see Appendix
II for all of the functions and what they do.
String
The String library contains functions for handling strings.
In WMLScript a string is essentially treated as an array of characters, with the
first element at offset of 0. The String library contains 16 functions, all
which are listed in Appendix II.
The charAt function requires a string and an
integer as parameters. It basically returns a string of the character from the
input string, at the location specified by the integer. Example 6.3 shows the use of the charAt function. Figure 6.3 shows the associated screen.
The find function from the String library is
used to find from the first parameter string the part that is indicated by the
second parameter string. If the part string is found in the target string the
function returns an integer, which represents the index of the first character
in the second parameter string. If the given partial string is not found, the
function returns the value–1.
Figure 6.4 shows
the screen associated of example
6.4.
Example 6.4 shows
the use of the find function.
The isEmpty function returns the
value of true, if the length of the parameter string is 0, in other cases it
returns the value false.
Example 6.5 shows
this function, and figure 6.5
shows the associated screen.
The other functions in the String library are covered in Appendix II.
Lang
The Lang library has functions that have a significant link
to the WMLScript language. Library functions can be used to check variable
types, imply type conversions, and influence the execution of the script.
There are 15 functions in the Lang library. They are all listed in
AppendixII.
The abs function of the Lang library returns the
absolute value of a parameter value. The parameter value can be either an
integer or a floating- point value, the function returns the same type of value.
Example 6.6 shows the use of the
abs function.Figure
6.6 shows the associated screen.
The isFloat function returns the value true if
it is able to convert a parameter to floating-point by using the parseFloat function. Strings can be converted to floating-point
values if they have a floating-point presentation format.
Integers are converted directly to their corresponding
floating-point values. (e.g. the integer 5 becomes the floating-point value
5.0.). If a parameter cannot be converted to a floating-point value, the
function returns the value of false.
Example 6.7 shows
the use of the isFloat function in the Lang library. The Figure 6.7 shows the associated
screen.
All of the functions in the Lang library are listed in Appendix II.
Float
The Float library routines deal with the processing of
floating-point values. The library contains the most typical functions that are
needed to execute floating-point calculations, but also calculations involving
integer values.
All of the functions in the Float library are listed in Appendix II, but
for an example of one, consider the pow function. This pow function from the Float library returns the floating-point
value which is the value of the first parameter (base) to the power of the
second parameter (exponent). Example6.8 shows this function. Figure 6.8 shows the associated
screen.
The round function is another Float library
function. It returns an integer that is the closest approximation of the entered
integer or floating-point parameter value. If the parameter is an integer, it
will be returned as such. If the parameter is a floating-point value, the round
function will use common mathematical approximation rules when converting it to
the closet possible integer. Example
6.9 shows the use of the round function.
URL
The URL library contains functions that can be used to
examine validation, content and parameters of absolute and relative URL
addresses. The library also includes functions for searching the contents of new
URL addresses.
The escapestring function from the URL library
returns from the entered parameter a new string that is URL encoded. This means
that all special characters have been changed to hexadecimal format. This method
is used when sending form data to CGI programs and Java servlets using the POST
or GET methods. Example 6.10 uses
the escapestring function. The figure shows the associated
screen
Just a note on inputting variables from a WML program to a
WMLScript function: you need single quotes around the variable (e.g. ‘$(str)’ )
if it is a string, but you don’t need the single quotes around the variable if
it is an integer or floating-point.
The getbase function from the URL library
returns the absolute address of the WMLScript compilation unit. Example 6.11 shows the use of the get- base function. Figure 6.11 shows the associated screen
WML Browser
The WMLBrowser library functions have been used in most of
the examples in Chapters 3, 4, 5, and 6. The library contains functions which make it possible to
examine WML document variables, to change their values and to manipulate the
transfer between documents in a browser.
A variable that is defined in a WML document can be retrieved into
a WMLScript function by using the getVar function from the
WMLBrowser library. The variable name used in the WML document is the same
variable name used in the WMLScript compilation unit. Basically this is another
way to “pass” variables from WML documents and WMLScript functions, without
listing them as parameters in the function call.
Example 6.12 shows
the use of the getVar function. Figure 6.12 shows the associated screen.
The go function in the WMLBrowser library
defines the URL address where the browser moves when the control of the program
execution transfers from the WMLScript interpreter back to the browser. Normally
the go function is called as the last WMLscript command.
Example 6.13 shows
the use of the go function as it appears in a WMLScript
function.
The setVar and refresh functions from the WMLBrowser library are used in most of
the examples in previous chapters. They set the value of variables (setVar) and refresh the contents of the browser(refresh).
The remaining functions of the WMLBrowser library are listed
in AppendixII.
Dialogs
The Dialogs library in WMLScript has useful functions, which
can be used to quickly create interactive dialog windows for the user. The
library also contains functions that display user entries in the dialog window.
The alert function from the Dialogs library
creates a message on the browser display, and expects the user to respond to it
by pushing the ok button.
Example 6.14 shows
the use of the alert function. Figure
6.14 shows the associated screen.
The confirm function from the Dialogs library
creates a message on the browser screen and expects the user to respond before
continuing the program execution. Example 6.15 shows the use of the confirm functions.
Figure 6.15 shows the associated
screen.
The remaining functions in the Dialogs library are listed in
Appendix II.