Transcript

Comparing VBScript control structures to Comparing VBScript control structures to JavaScript control JavaScript control structuresstructures

http://www.apextgi.in

Data Types and Control StructuresData Types and Control Structures

JavaScript   VBScript 

1 if ( s == t ) 2 u = s + t; 3 else if ( s > t ) 4 u = r; 5 else 6 u = n;

1 If s = t Then 2 u = s + t 3 ElseIf s > t Then 4 u = r 5 Else 6 u = n 7 End If

JavaScript   VBScript 

1 switch ( x ) { 2 case 1: 3 alert("1"); 4 break; 5 case 2: 6 alert("2"); 7 break; 8 default: 9 alert("?"); 10 }

1 Select Case x 2 Case 1 3 Call MsgBox("1") 4 Case 2 5 Call MsgBox("2") 6 Case Else 7 Call MsgBox("?") 8 End Select

Comparing JavaScript’s if structure to VBScript’s If structure

Comparing JavaScript’s switch to VBScript’s Select Case

Data Types and Control StructuresData Types and Control Structures

JavaScript   VBScript 

1 while ( !( x == 10 ) ) 2 ++x;

1 Do Until x = 10 2 x = x + 1 3 Loop

JavaScript VBScript

1 do { 2 ++x; 3 } while ( !( x == 10 ) );

1 Do 2 x = x + 1 3 Loop Until x = 10

JavaScript   VBScript 

1 x = 8; 2 for ( y = 1; y < x; y++ ) 3 x /= 2;

1 x = 8 2 For y = 1 To x 3 x = x \ 2 4 Next

Comparing JavaScript’s while to VBScript’s Do Until

Comparing JavaScript’s do/while to VBScript’s Do Loop/Until

Comparing JavaScript’s for to VBScript’s For

Data Types and Control Data Types and Control StructuresStructures◦ Select Case/End Select

Does not require break type statement

◦ VBScript structures without direct JavaScript equivalents: Do Until/Loop Do/Loop Until Loop until condition becomes True

◦ Exit Do Immediate exit from Do While/Loop, Do/Loop While, Do

Until/Loop or Do/Loop Until

◦ Exit For Immediate exit from For/Next

◦ For loop Optional Step keyword to increment or decrement

1 ’ VBScript2 For y = 2 To 20 Step 23 Call MsgBox( "y = " & y )4 Next

VBScript FunctionsVBScript Functions Predefined functions

◦ Variant functions IsEmpty

Returns True if variant not initialized

◦ Math functions Cos, Sin, etc.

Take arguments in radians radians = degrees π/180

◦ InputBox Displays dialog in which user can input data

◦ MsgBox Displays message dialog

◦ VBScript functions often take optional arguments

◦ Formatting functions

FormatCurrency, FormatDateTime, etc.

VBScript FunctionsVBScript Functions

◦ Functions for getting info about scripting engine ScriptEngine

Returns “Jscript”, “VBScript” or “VBA” ScriptEngineBuildVersion

Returns current build version; ID number for current release ScriptEngineMajorVersion

Returns major version number for script engine ScriptEngineMinorVersion

Returns minor release number Line continuation character◦ Underscore character, _◦ Statements cannot extend beyond current line without character

VBScript Example ProgramsVBScript Example Programs

Always place VBScript code inside HTML comments

◦ Prevent code from being displayed as text in browsers that do not understand VBScript

Script variables

◦ Variables declared outside of procedures Const keyword

◦ Create constants

VBScript Example ProgramsVBScript Example Programs

Comments

◦ Single quote (‘)

◦ Keyword Rem (remark) Considered outdated

Procedures:

◦ Sub keyword Procedure that does not return a value Exit Sub exits Sub procedure

◦ Function keyword Procedure that returns a value Exit Function exits Function procedure

VBScript Example ProgramsVBScript Example Programs

Function InputBox

◦ Displays a dialog for user to input data

Call InputBox ( prompt, caption, help File,_ x-coord, y-coord)

◦ Coordinates measured from top left (0,0). Measured in twips (1440 twips = 1 inch)

◦ Optional parameter help File can be left out by writing consecutive commas , ,

◦ _ (underscore) - line continuation character, required if statement extends beyond a line Use as many as necessary

VBScript Example ProgramsVBScript Example Programs Calling functions

◦ If function call has arguments in parentheses, use keyword Call If function assigns a variable, Call not needed

a = Abs( z ) If parentheses not used, keyword Call not needed

Function MsgBox

◦ Displays message dialog

MsgBox "VBScript is fun!", , "Results"

◦ Displays "VBScript is fun!" with "Results" in the title bar

◦ Optional argument to customize buttons and icon ignored

1.1Set language to VBScript

1.2Option Explicit statement

1.3Define procedure On Click for the cmAdd button

1.4Use CInt to convert input values from string subtype to integer subtype

1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2<HTML>3<!--Fig. 22.15: addition.html -->5<HEAD>6<TITLE>Our first VBScript</TITLE>78<SCRIPT LANGUAGE = "VBScript">9<!--10 Option Explicit11 Dim intTotal1213 Sub cmdAdd_OnClick()14 Dim intValue15 16 intValue = InputBox("Enter an integer", "Input Box", , _17 1000, 1000)18 intTotal = CInt( intTotal ) + CInt( intValue )19 Call MsgBox("You entered " & intValue & _20 "; total so far is " & intTotal, , "Results")21 End Sub22-->23</SCRIPT>24</HEAD>2526<BODY>27Click the button to add an integer to the total.28<HR>

29<FORM>

30<INPUT NAME = "cmdAdd" TYPE = "BUTTON" 31 VALUE = "Click Here to Add to the Total">32</FORM>33</BODY>34</HTML>

Adding integers on a Web page using VBScriptAdding integers on a Web page using VBScript

Input dialog

Message dialog

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on VB Script and JAVA Script

http://www.apextgi.in


Top Related