Chapter: Internet & World Wide Web HOW TO PROGRAM - VBScript

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

VBScript Example Programs

In this section, we present several complete VBScript “live-code” programs and show the screen inputs and outputs produced as the programs execute.

VBScript Example Programs

 

In this section, we present several complete VBScript “live-code” programs and show the screen inputs and outputs produced as the programs execute. The XHTML document of Fig. 24.15 includes VBScript code that enables users to click a button to display an input dialog in which they can type an integer to be added into a running total. When the input dialog’s OK button is clicked, a message dialog is displayed with a message indicating the number that was entered and the total of all the numbers entered so far.

 

          <?xml version = "1.0"?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

         

          <!--Fig. 24.15: addition.html -->

          <!--Adding Integers        -->

          <html xmlns = "http://www.w3.org/1999/xhtml">

          <head>

          <title>Our first VBScript</title>

         

          <script type = "text/vbscript">

          <!--

          Option Explicit

          Dim intTotal

         

          Sub cmdAdd_OnClick()

          Dim intValue

         

          intValue = InputBox(_

          "Enter an integer", "Input Box", , 1000, 1000)

          intTotal = CInt( intTotal ) + CInt( intValue )

          Call MsgBox("You entered " & intValue & _

          "; total so far is " & intTotal, , "Results")

          End Sub

          -->

          </script>

          </head>

 

          <body>

          Click the button to add an integer to the total.

          <hr />

          <form action = "">

          <input name = "cmdAdd" type = "button"

          value = "Click Here to Add to the Total" />

          </form>

          </body>

          </html>

 


Fig. 24.15  Adding integers on a Web page using VBScript

 

On Line 12, the XHTML tag script sets the type attribute to vbscript. This tag tells the browser to use its built-in VBScript interpreter to interpret the script code. Notice the XHTML comment tags on lines 13 and 26 which appear to “comment out” the VBScript code.

If the browser understands VBScript, these XHTML comments are ignored, and the VBScript is interpreted. If the browser does not understand VBScript, the XHTML com-ment prevents the VBScript code from being displayed as text.

Line 14 uses the Option Explicit statement to force all variables in the VBScript code to be declared. Statement Option Explicit, if present, must be the first statement in the VBScript code. Line 15 declares variant variable intTotal, which is visible to all procedures within the script. Variables declared outside of procedures are called script variables.

Lines 17–25 define a procedure (i.e., VBScript’s equivalent of a function in JavaS-cript) called OnClick for the cmdAdd button. VBScript procedures that do not return a value begin with the keyword Sub (line 17) and end with the keywords End Sub (line 25). We will discuss VBScript procedures that return values later in this chapter. Line 18 declares the local variable intValue. Variables declared within a VBScript procedure are visible only within that procedure’s body. Procedures that perform event handling (such as the cmdAdd_OnClick procedure in lines 17–25) are more properly called event pro-cedures.

 

Line 20 calls the function InputBox to display an input dialog. The value entered into the input dialog is assigned to the intValue variable and is treated by VBScript as a string subtype. When using variants, conversion functions are often necessary to ensure that you are using the proper type. Line 22 calls VBScript function CInt twice to convert from the string subtype to the integer subtype. VBScript also provides conversion func-tions CBool for converting to the boolean subtype, CByte for converting to the byte subtype, CCur for converting to the currency subtype, CDate for converting to the date/ time subtype, CDbl for converting to the double subtype, CLng for converting to the long subtype, CSng for converting to the single subtype and CStr for converting to the string subtype. Lines 23–24 display a message dialog indicating the last value input and the running total.

 

VBScript provides many predefined constants for use in your VBScript code. The con-stant categories include color constants, comparison constants (to specify how values are compared), date/time constants, date format constants, drive type constants, file attribute constants, file I/O constants, MsgBox constants, special folder constants, string constants, VarType constants (to help determine the type stored in a variable) and miscellaneous other constants. VBScript constants usually begin with the prefix vb. For a list of VBScript constants, see the VBScript documentation. You can also create your own constants by using keyword Const, as in

const PI = 3.14159

 

Figure 24.16 provides another VBScript example. The XHTML form provides a select component, to allow the user to select a Web site from a list of sites. When the selection is made, the new Web site is displayed in the browser. Lines 34–39 specify a VBScript. In such code, the <script> tag’s for attribute indicates the XHTML compo-nent on which the script operates (SiteSelector), the event attribute indicates the event to which the script responds (OnChange, which occurs when the user makes a selec-tion) and the type attribute specifies the scripting language (VBScript).

 

 

      <?xml version = "1.0"?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

      <!-- Fig. 24.16: site.html -->

      <!-- Displaying a Web site -->

 

      <html xmlns = "http://www.w3.org/1999/xhtml">

      <head>

        <title>Select a site to browse</title>

        </head>

               <body>

               Select a site to browse<p>

               <hr />

               <form action = "">

               <select name = "SiteSelector" size = "1">

              

               <option value = "http://www.deitel.com">

               Deitel & Associates, Inc.

               </option>

              

               <option value = "http://www.prenhall.com">

               Prentice Hall

               </option>

              

               <option value = "http://www.phptr.com/phptrinteractive">

               Prentice Hall Interactive

               </option>

              

               </select>

              

               <!-- VBScript code -->

               <script for = "SiteSelector" event = "onchange"

               type = "text/vbscript">

               <!--

               Document.Location = Document.Forms( 0 ).SiteSelector.Value

               -->

               </script>

               </form></p>

               </body>

               </html>

         



Fig. 24.16 Using VBScript code to respond to an event. (Courtesy of Prentice Hall, Inc.)

 

Line 37 causes the browser to change to the selected location. This line uses Internet Explorer’s Document object to change the location. The Document object’s Location property specifies the URL of the page to display. The expression SiteSe-lector.Value gets the value of the selected option in the select. When the assignment is performed, Internet Explorer automatically loads and displays the Web page for the selected location.

Fig. 24.17 uses programmer-defined procedures: Minimum, to determine the smallest of three numbers; and OddEven, to determine whether the smallest number is odd or even.

 

          <?xml version = "1.0"?>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

         

          <!--Fig. 24.17: minimum.html -->

          <!-- VBScript Procedures        -->

 

          <html xmlns = "http://www.w3.org/1999/xhtml">

          <head>

          <title>Using VBScript Procedures</title>

         

          <script type = "text/vbscript">

          <!--

          Option Explicit

         

          ' Find the minimum value. Assume that first value is

          ' the smallest.

          Function Minimum( min, a, b )        

                            

          If a < min Then             

          min = a                

          End If                  

                            

          If b < min Then             

          min = b                

          End If                  

                            

          Minimum = min   ' Return value      

          End Function                

                            

          Sub OddEven( n )         

          If n Mod 2 = 0 Then     

          Call MsgBox( n & " is the smallest and is even" )

          Else            

          Call MsgBox( n & " is the smallest and is odd" )

          End If                  

          End Sub              

                            

          Sub cmdButton_OnClick()     

          Dim number1, number2, number3, smallest       

                            

          ' Convert each input to Long subtype       

          number1 = CLng( Document.Forms( 0 ).txtBox1.Value )

          number2 = CLng( Document.Forms( 0 ).txtBox2.Value )

          number3 = CLng( Document.Forms( 0 ).txtBox3.Value )

                            

          smallest = Minimum( number1, number2, number3 )   

          Call OddEven( smallest )        

          End Sub              

          -->

          </script>

          </head>

 

          <body>

          <form action = ""> Enter a number

          <input type = "text" name       = "txtBox1" size = "5"

          value = "0" />      

          <p>Enter a number      

          <input type = "text" name       = "txtBox2" size = "5"

          value = "0" /></p>        

          <p>Enter a number      

          <input type = "text" name       = "txtBox3" size = "5"

          value = "0" /></p>        

          <p><input type = "button"     name = "cmdButton"

          value = "Enter" /></p> 

                  

          </form>

          </body>

          </html>


 

Fig. 24.17  Program that determines the smallest of three numbers

 

Lines 16–17 are VBScript single-line comments. VBScript code is commented by either using a single quote (') or the keyword Rem (for remark) before the comment. [Note: Keyword Rem can be used only at the beginning of a line of VBScript code.]

Lines 18–29 define the programmer-defined procedure Minimum. VBScript proce-dures that return a value are delimited with the keywords Function (line 18) and End Function (line 29). This procedure determines the smallest of its three arguments by using If/Then/Else structures. A value is returned from a Function procedure by assigning a value to the Function procedure name (line 28). A Function procedure can return only one value.

 

Procedure OddEven (lines 31–37) takes one argument and displays a message dialog indicating the smallest value and whether or not it is odd or even. The modulus operator Mod is used to determine whether the number is odd or even. Because the data stored in the variant variable can be viewed as a number, VBScript performs any conversions between subtypes implicitly before performing the modulus operation. The advantage of placing these procedures in the head is that other VBScripts can call them.

 

Lines 39–49 define an event procedure for handling cmdButton’s OnClick event. The statement in line 47 calls Minimum, passing number1, number2 and number3 as arguments. Parameters min, a and b are declared in Minimum to receive the values of number1, number2 and number3, respectively. Procedure OddEven is passed the smallest number, on line 48.

 

One last word about procedures—VBScript provides statements Exit Sub and Exit Function for exiting Sub procedures and Function procedures, respectively. Control is returned to the caller and the next statement in sequence after the call is executed.


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


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