WML and Wap BT0087 Part-2 discusses WML scripting language features such as comments, variables, functions, operators, and library functions for handling URLs. The document provides examples and explanations of WML script syntax, including function declarations and arguments, return statements, and the extern keyword. It also describes library functions like isValid(), getScheme(), getHost(), getPath(), and loadString() for validating, parsing, and loading URLs.
2. 2
1. What are the problems in WML?
WML which is infested with myriad problems. They are as follows: (a) it is static in
nature. (b) Even though it has the concept of variables, initializing these variables is not as simple as it
is in any other programming languages. (c) It doesn't support the features that other programming
languages do. As a consequence, you cannot write a code that is dynamic. Dynamic in this context
means, the ability to modify the behavior of the program while it is running. Therefore, WML is similar
to HTML. In both cases, you cannot write programs, which have sufficient inbuilt intelligence.WML
also fails to offer you the requisite level of flexibility.
2. What are the features of WML script Language?
Some basic syntactical features of the language are:
The smallest unit of execution in WMLScript is a statement and each statement must end with a
semicolon (;).
The following code is taken from the earlier "Hello World" example. You can see that the two
WMLScript statements in this function have been ended with semicolons.
extern function helloWorld()
{
WMLBrowser.setVar("message", "Hello World. Welcome to our WMLScript
tutorial.");
WMLBrowser.refresh();
}
WMLScript is case-sensitive.
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.
Comments can either be single-line (beginning with //) or multi-line (bracketed by /* and */). This
syntax is identical to both C++ and Java.
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
3. 3
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.
}
A literal character string is defined as any sequence of zero or more characters enclosed within double
("") or single (‘) quotes.
Boolean literal values correspond to true and false.
New variables are declared using the var keyword (i.e. var x ;)
3. What is an operator? What are the operators supported by WML Script? Explain with
examples.
An operator is essentially a predefined function that is a part of the WMLScript language.
WMLScript supports a variety of operators that support value assignment operations, arithmetic
operations, logical operations, string operations, comparison operations, and array operations
WML Script offers several ways to assign a value to a variable. The simplest is the regular assignment
(=), but assignments with operations are also supported:
4. What are the Characteristics of the functions?
Characteristics of the functions are The characteristics of the functions in WML Script are
mentioned below
Function declarations cannot be nested.
Function names must be unique within one compilation unit.
All parameters to functions are passed by value.
4. 4
Function calls must pass exactly the same number of arguments to the called function as specified
in the function declaration.
Function parameters behave like local variables that have been initialized before the function
body (block of statements) is executed.
A function always returns a value. By default, it is an empty string (" ").
However, you can use a return statement to specify other return values. 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)
5. 5
{
...
}
If a function does not require any arguments, you still need to include the
parentheses (), like this:
unction 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);
6. 6
}
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 compulsory 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.
5. Which are the library functions that handle absolute URLs? Explain.
Isvalid, getScheme, getHost, getPort, getPath are some of the functions which handles absolute URLs
This library contains a set of functions for handling absolute URLs and
relative URLs. The general URL syntax supported is:
<scheme> ://< host > :< port >/ < path >; < params>? <query >#< fragment>
The following functions: loadString ,unescapeString ,escapingString,
resolve,getRefer ,getBase ,getFragment ,getQuery ,getParameters ,getPath
,getPort ,getHost ,getScheme and isValid which comes under URL Library is discussed in this section
7. 7
isValid
Function : isValid(url)
Description : Returns true if the given url has the right URL syntax,
otherwise returns false.
Both absolute and relative URLs are supported.
Relative URLs are not resolved into absolute URLs.
Parameters : url=String
Return value : Boolean or invalid.
: –
Exceptions
Example : vara=URL.isValid(http://www.webcomtec.com/script#func());
II a = true
var b = URL.isValid("../common#test()");
II b = true
Varc= URL.isValid("experimental?://www.webcomtec.com/pub")
II c = false
getScheme
Function : getScheme (url)
Description : Returns the scheme used in the given url.
Both absolute and relative URLs are supported.
Relative URLs are not resolved into absolute URLs.
Parameters : url=String
8. 8
Return value : String or invalid
Exceptions : if an invalid URL syntax is encountered while extracting the
scheme, an invalid value is returned.
Example : Var a = URL.isValid(http://w.a.com"); //a = "http"
Var b = URL.getScheme("w.a.com"); II b = . ""
getHost
Function : getHost(url)
Description : Returns the host specified in the given url.
Both absolute and relative URLs are supported.
Relative URLs are not resolved into absolute URLs.
Parameters : url=String
Return value : String or invalid
Exceptions : If an invalid URL syntax is encountered while extracting the
host part, an invalid value is returned.
Example : var a= URL.getHost("http://www.webcomtec.com/pub");
II a = "www.webcomtec.com"
var b = URL.getHost(".path#frag") ;
II b = " "
getPort
Function : getPort(url)
Description : Returns the port number specified in the given url.
If no port is specified, an empty string is returned. Both absolute and relative URLs
9. 9
are supported. Relative URLs are not resolved into absolute URLs.
Parameters : url=String
Return value : String invalid
Exceptions : If an invalid URL syntax is encountered while extracting the
port number, an invalid value is returned.
Example : var a = URL.getport(http://www.webcomtec.com:80/path");
II a= "80"
var b = URL.getport("http:/www.webcomtec.com/path");
II b = ""
getPort
Function : getPort(url)
Description : Returns the port number specified in the given url.
If no port is specified, an empty string is returned. Both absolute and relative URLs
are supported. Relative URLs are not resolved into absolute URLs.
Parameters : url=String
Return value : String invalid
Exceptions : If an invalid URL syntax is encountered while extracting the
port number, an invalid value is returned.
Example : var a = URL.getport(http://www.webcomtec.com:80/path");
II a= "80"
var b = URL.getport("http:/www.webcomtec.com/path");
II b = ""
getPath
10. 10
Function : getPath(url)
Description : Returns the path specified in the given url. Both absolute and relative URLs are
supported. Relative URLs are not resolved into absolute URLs.
Parameters : url=String
Return value : String or invalid.
Exceptions : If an invalid URL syntax is encountered while extracting the
path, an invalid value is returned.
Example : var a= URL.getpath(http://w.a.com/home/sub/comp#frag");
II a = "/home/sub/comp"
var b = URL.getPath("../home/sub/comp#frag");
II b = "../home/sub/comp"
getParameters
Function : getParameters(url)
Description : Returns the parameters used in the given url.
If no parameters are specified an empty string is returned.
Both absolute and relative URLs are supported.
Relative URLs are not resolved into absolute URLs.
Parameters : url = String
Return value : String or invalid.
Exceptions : If an invalid URL syntax is encountered while extracting the
parameter, an invalid value is returned.
Example : var a = URL. getParameters("http://w.a.c/scr;3;2?x=1 &
11. 11
y=3");
// a = " 3 ; 2"
var b = URL.getParameters
("../scr;3;2?x=1&y=3") ; II b = "3;2"
getQuery
Function : getQuery(url)
Description : Returns the query part specified in the given url.
If no query part is specified an empty string is returned. Both absolute and relative
URLs are supported. Relative URLs are not resolved into absolute URLs.
Parameters : url = String
Return value : String or invalid
Exceptions : If an invalid URL syntax is encountered while extracting the
query part, an invalid is returned.
Example : var a = URL. getQuery (http://w.a.c/scr;3;2?x=1&y=3");
II a = "x = 1&y = 3"
getFragment
Function : getFragment(url)
Description : Returns the fragment used in the given url.
If no fragment is specified an empty string is returned. Both absolute and relative URLs are
supported.
Relative URLs are not resolved into absolute URLs.
Parameters : url=String
12. 12
Return value : String or invalid
Exceptions : IF an invalid URL syntax is encountered while extracting the
fragment, an invalid value is returned.
Example : vara=URL.getFragment("http://www.webcomtec.com/cont#frag");
II a = "frag"
getBase
Function : getBase(url)
Description : Returns an absolute URL (without the fragment) of the
current WML Script compilation unit.
Parameters : –
Return value : String
: –
Exceptions
Example : var a= URL. GetBase ( );
getRefer
Function : getRefer(url)
Description : Returns the smallest relative URL (relative to the base URL
of the current compilation unit) to the resource that called the current compilation
unit. Local function calls do not change the referer. If the current compilation unit does not have a
referer, an empty string is returned.
13. 13
Parameters : –
Return value : String
–
Exceptions :
Example : var base= URL.getBase( );
//base
="http://www.webcomtec.com/currrent .scr"
var referer = URL.getReferer () ;
//referer = app.wml"
resolve
Function : resolve (baseurl, embedded URL)
Description : Returns an absolute URL from the given base URL and the embedded URL. If the
embedded URL is already an absolute URL, the function returns it without modification.
Parameters : baseUrl=String
Embedded Url=String
Return value : String or invalid
Exceptions : If an invalid URL syntax is encountered as part of the
resolution, invalid value is returned.
Example : var a= URL.resolve
II a= invalid
escapingString
Function : escapeString(url)
14. 14
Description : This function computes a new version of a string value in
which special characters are replaced by a hexadecimal
escape sequence (you must use a two-digit escape
sequence of the form %xx).
Parameters : String=String
Return value : String or invalid
Exceptions : If string contains characters that are not part of the US-
ASCII character set, an invalid is returned.
unescapeString
Function : unescapeString (url)
Description : The unescape function computes a new version of a string value in which each
escape sequence of the sort that might be introduced by the URL.escapeString () function is
replaced by the character it represents. The given string is unescaped as such; no URL parsing is
performed.
Parameters : String=String
Return value : String or Invalid
Exceptions : If string contains characters that are not part of the US-
ASCII character set, an invalid is returned.
loadString
Function : loadString(url,content Type)
Description : Returns the content denoted by the given absolute url and the content Type.
The given content type is erroneous if it does not follow the following rules: You can specify only
15. 15
one content type. The whole string 3 must match with only one content type and you cannot have
any extra leading or trailing spaces. The type must be text, but the subtype can be anything.
The type prefix must be "text/"
The given behaves as follows:
The content with the given content Type and url is loaded. The rest of the attributes needed
for the content load are specified by the default settings of the user agent. If the load is successful or
the returned content type matches the given content Type, the content is converted to a string and
returned. If the load is unsuccessful or the returned content is of the wrong content type, a scheme-
specific error code is returned.
Parameters : url=String
ContentType=String
Return value : String, integer or invalid.
Exceptions : Returns an integer error code that depends on the used URL scheme if the load fails.
If Http or WSP schemes are used, HTTP error codes are returned. If an erroneous content
Type is given, an invalid value is returned.
Example : varmyUrl = http://www.webcomtec.com/vcards/myaddr. vcf;
MyCard = URL.loadString(myUrl, "text/x-vcard");