Chapter: Internet & World Wide Web HOW TO PROGRAM - Wireless Internet and m-Business

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

WMLScript Programming

We now begin our introduction to the WMLScript scripting language. WMLScript facilitates a disciplined approach to the designing of programs that enhance the functionality of WML documents.

WMLScript Programming

 

We now begin our introduction to the WMLScript scripting language. WMLScript facilitates a disciplined approach to the designing of programs that enhance the functionality of WML documents. The relationship between WML and WMLScript is similar to that be-tween XHTML and JavaScript. However, one key difference is that WMLScript is placed in a separate document and cannot be embedded inside a WML document.

 

Our first WMLScript example provides the text “Welcome to WMLScript Pro-gramming! to a WML document. Openwave’s browser contains a WMLScript inter-preter for the execution of WMLScript commands. Our first script is shown in Fig. 23.6; the associated WML document and output are shown in Fig. 23.7.

 

    // Fig. 23.6: welcomeDoc.wmls

    // Writing a line of text

    extern function welcome()

    {

    // creating a browser variable and assigning it a value

    WMLBrowser.setVar( "welcome",

    "Welcome to WMLScript programming!" );

      // refresh the display window

      WMLBrowser.refresh();

}

Fig. 23.6 WMLScript listing for welcomeDoc.wmls.

 

Lines 4–12 define function welcome. Keyword extern denotes that the function is externally accessible to other WML and WMLScript documents. The omission of this key-word restricts the function’s visibility (or scope) to the WMLScript file in which it is defined. Functions that do not use extern are called utility or helper functions. These functions often contain logic that is specific to the WMLScript file. Other external docu-ments cannot not call these functions directly.

 

WMLScript provides objects for performing common mathematical calculations, string manipulations, browser manipulations and other functions. These objects offer many basic capabilities that programmers need. WMLScript provides the WMLBrowser object for interacting with the browser. We call the WMLBrowser object’s setVar method

 

(lines 7–8) to create a browser variable named welcome and to assign it a string. Browser variables are global variables; they reside in the browser’s memory and are accessible to any WML or WMLScript document residing in the browser’s memory.

 

Line 11 calls the WMLBrowser object’s refresh method to update (or refresh) the values of all browser variables. This allows the WML document that calls function wel-come to use the browser variable’s new value. In this instance, invoking the refresh method refreshes the browser, updating welcome’s value. If the variable is not refreshed, browser variable welcome will display an empty string when it is dereferenced in the WML document (line 19 of Fig. 23.7).

The WML document in Fig. 23.7 invokes function welcome in welcomeDoc.wmls (Fig. 23.6). The result of function welcome is displayed by the WML document.

 

            <?xml version = "1.0"?>

            <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"

            "http://www.wapforum.org/DTD/wml12.dtd">

           

            <!-- Fig. 23.7: fig23_7.wml  -->

            <!-- Printing a line of text -->

           

            <wml>

 

            <card id = "Line" title = "Line">

            <onevent type = "onenterforward">

            <!-- call function welcome -->

            <go href = "welcomeDoc.wmls#welcome()" />

            WML document that calls function welcome (part 1 of 2). (Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.)

            15                              

            </onevent>             

 

            <p>                

            $welcome <!-- dereference browser variable welcome -->

            </p>

            </card>

            </wml>


 

Lines 11–16 contain the onevent element that invokes WMLScript function wel-come. The onevent element is an event element that executes a task element, such as the go element (line 14), which is wrapped in its tags. Task elements such as go, refresh and prev perform certain actions when executed. A complete list of task elements can be found at www.w3schools.com/wap/wml_reference.asp. Attribute type is set to "onenterforward". This executes the task element go when the card is loaded.

 

Function welcome in welcomeDoc.wmls is invoked in line 14 by assigning to attribute href the WMLScript document name followed by a # sign and the function name (Fig. 23.6). WMLScript documents have the .wmls file extension and are referenced from within a WML document.

 

Lines 18–20 mark up browser variable welcome’s value with <p> tags. The insertion of the dollar sign ($) before the variable name retrieves the browser variable’s value from the device’s memory.

 

Sometimes it is useful to display important messages, such as those that inform users that required form fields have been left blank, in windows called dialogs. Function dis-playDialog (Fig. 23.8) displays text in an alert dialog. This function is called from within the WML document shown in Fig. 23.9.

 

The Dialogs object contains methods for the displaying of messages on clients devices. Line 6 (Fig. 23.8) calls Dialogs method alert, which displays an alert dialog (Fig. 23.9). The string passed to this method is displayed to the user. The dialog output dis-plays three lines of text. As each newline character (\n) is rendered, subsequent text is dis-played on the next line.

 

    // Fig. 23.8: dialogPrompt.wmls

    // Printing multiple lines in a dialog

 

    extern function displayDialog()

    {

    Dialogs.alert( "Welcome to\nWMLScript\nProgramming!" );

}

 

 

Fig. 23.8 WMLScript listing for dialogPrompt.wmls.

 

Figure 23.9 is the WML document that invokes function displayDialog. Most browsers contain one or more soft keys that allow users to select options. Soft keys are the physical buttons on a wireless device that enable a user to navigate between documents. By default, the alert dialog labels the left soft key OK. When the user presses the soft key, the dialog is closed (or dismissed). After the dialog is closed, any remaining WML markup is ren-dered.

 

In our next example, we explore WMLScript functions in greater detail. Figure 23.10 calls a programmer-defined function, count, to obtain a number from the user, convert the number to an integer and pass that value to a second programmer-defined function, square. This function returns the square of the integer.

 

Lines 7–8 call Dialogs method prompt to obtain a number from the user. The first argument passed to method prompt is the prompt message that is displayed to the user. The second argument specifies a default value for which we provide an empty string value. The default value is displayed in the input field when the dialog opens. If a default value is supplied, the user would have to delete this value to enter information. Method prompt creates a soft key labeled alpha, which limits the information that users can enter to letters typed via the device’s keypad. Variable inputNumber stores the string input by the user. In line 12, we call Lang object method parseInt to convert inputNumber’s value from a string to an integer. Object Lang provides methods for conversion between data types and for the performing of mathematical calculations.

 

There are two ways to declare variables in WMLScript: Method setVar and keyword var. Method setVar declares a browser variable that can be accessed by both WML and WMLScript documents, and keyword var declares a local variable (i.e., a variable that can be accessed only in the function in which it is declared).

 

            <?xml version = "1.0"?>

            <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN"

            "http://www.wapforum.org/DTD/wml12.dtd">

                       

            <!-- Fig. 23.9: fig23_9.wml -->

            <!-- Using dialogs   -->

                       

            <wml>

            <card id = "Dialog" title = "Dialog">

           

            <!-- event element to execute go element -->

            <onevent type = "onenterforward">

                       

            <!-- call function displayDialog -->

            <!-- in dialogPrompt.wmls          -->

            <go href = "dialogPrompt.wmls#displayDialog()" />

                       

            </onevent> 

                       

            </card>

            </wml>


Fig. 23.9 Displaying multiple lines in a dialog. (Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.)

 

    // Fig. 23.10: squareNumbers.wmls

    // Programmer-defined functions

 

    extern function count()

    {

    // prompt the user for a number

    var inputNumber = Dialogs.prompt(

    "Enter a number to be squared", "" );

 

      // convert the number to an integer and pass

      // the number to function square

      var numberSquared = square( Lang.parseInt( inputNumber ) );

      var outputSquare = inputNumber + " squared is " +

      numberSquared;

16

      // set the string to a browser variable and

      // redirect the client to the card named result

      WMLBrowser.setVar( "result1", outputSquare );

      WMLBrowser.go( "#result" );

      }

22

      function square( y )

      {

      return y * y;

      }

       

 

Fig. 23.10  Using programmer-defined functions to square a number

 

Function square (line 23) calculates the square of a number and returns the result to function count. The scope of function square is restricted to squareNum-bers.wmls, because keyword extern has been omitted from the definition. Lines 14– 15 concatenate inputNumber’s value, the string " squared is " and the value of vari-able numberSquared. The result is then stored in variable outputSquare. Line 19 calls the WMLBrowser method setVar to create a new browser variable named result1, which is assigned the value of outputSquare. Line 20 calls WMLBrowser method go to load card result into the browser.

 

Figure 23.11 lists the WML document that contains the call to function count (Fig. 23.10). The document contains two cards. The first card programs a soft key (lines 12– 14), that, when pressed, invokes function count.

 

Element do (lines 12–14) programs a soft key for a wireless device. Attribute label defines the text that appears above the soft key on the display screen. Attribute type assigns an action to the soft key. When a device has two soft keys, the "accept" value programs the left soft keys and the "options" value programs the right soft key.

 

When a soft key is pressed, the go element’s action is executed. Line 13 contains the go element that calls function count in squareNumber.wmls. After the number is squared, the browser displays card result. Lines 24–26 program a soft key that dis-plays the previous card, which asks the user to enter the number to be squared. Element prev is a task element that displays the previous card.

 

In the previous examples, we used the WMLBrowser object for creating browser vari-ables. In Fig. 23.12, WMLBrowser method getVar is called to retrieve a variable’s value from a WML document.

 

Line 6 calls method getVar to obtain the value of browser variable username. This value is then assigned to local variable x. Figure 23.13 shows the WML document that defines browser variable username. Line 21 creates an input box for user input. The input element’s name attribute identifies the input box. An input box’s name becomes a browser variable once it is posted. The value of variable username is retrieved on line 6 of Fig. 23.12 and is assigned to variable x. We then call WMLBrowser method go to display card2 (Fig. 23.13). Lines 30–32 create a soft key that allows the user to return to the previous card to enter a new name. Line 35 displays the value of browser variable result.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.