VBSCRIPT:
Visual Basic Scripting Edition, also known as VBScript, enables authors to create scripts using a subset of the Microsoft Visual Basic language. If you are already a Visual Basic programmer, or if you are not a programmer but are looking for a scripting language that is easy to learn, VBScript might be the right language for you. VBScript is implemented as a fast, portable interpreter for use in Web browsers and applications that use ActiveX controls, Java applets, and OLE Automation servers.
VBScript is a strict subset of the Visual Basic for Applications language that is used in popular applications such as Microsoft Excel, Microsoft Access, Microsoft Project, and the Visual Basic 4.0 development system. VBScript was designed to be fast, so it does not support the use of strict types -- it only supports the use of Variants. It also must be safe for the World Wide Web, so it does not include functionality that directly accesses the client machine's operating system or file system. For example, you cannot do file I/O or read the registry on the client machine.
VBScript provides support for three separate classes of objects:
![]() |
Objects provided by the VBScript engine (the core run-time functionality with a minimal set of basic Visual Basic objects) |
![]() |
Objects provided by Internet Explorer |
![]() |
Objects provided by the VBScript author (that the Web author creates and/or inserts herself through the <OBJECT> HTML tag) |
Let's start with the classic Hello World
example using VBScript. The following code shows you how simple it is to create a button
(by using INPUT TYPE=BUTTON) that, when clicked (Sub BtnHello_OnClick), displays a
message box (MsgBox) with the text, "Hello, world!":
<CENTER> <P> <H2>Hello, world sample</H2> <INPUT TYPE=BUTTON VALUE="Click me" NAME="BtnHello"> </CENTER> <SCRIPT LANGUAGE="VBScript"> <!-- Sub BtnHello_OnClick MsgBox "Hello, world!", 0, "My first active document" End Sub --> </SCRIPT>
The Ordering Flowers example demonstrates how you can use VBScript to create a form and
gather and validate data given by the user. This form can be used for ordering flowers . It
uses radio buttons (INPUT TYPE=RADIO NAME) to enable the user to choose which type of card
to include with the flowers, text input fields (INPUT NAME=) to gather information on
where to send the flowers, and a button (INPUT TYPE=BUTTON) to submit the request. Here is
the HTML source code:
<INPUT TYPE=RADIO NAME=OptOccasion CHECKED> Birthday <INPUT TYPE=RADIO NAME=OptOccasion> Anniversary <INPUT TYPE=RADIO NAME=OptOccasion> Get well soon <FONT SIZE=3> <B>When and where should the flowers be sent?</B> </FONT> <BR> Date <INPUT NAME=TxtDate SIZE=60> Name <INPUT NAME=TxtName SIZE=60> Address <INPUT NAME=TxtAddress SIZE=60> City <INPUT NAME=TxtCity SIZE=60> State <INPUT NAME=TxtState SIZE=60> Zip code <INPUT NAME=TxtZip SIZE=60> <INPUT TYPE=BUTTON VALUE="Submit" NAME="BtnSubmit"> <INPUT TYPE=BUTTON VALUE="Clear" NAME="BtnClear"> <INPUT TYPE=BUTTON VALUE="Init" NAME="BtnInit"><BR>
Now that we have a place to put the information, VBScript code is used to validate the data. The fields are initialized when the window is loaded (Sub Window_OnLoad()) in the BtnInit_OnClick() function.
<SCRIPT LANGUAGE="VBScript"> <!-- Option Explicit Dim strMsgBoxTitle Dim bValidOrder Sub Window_OnLoad strMsgBoxTitle = "MSFTD" Call BtnInit_OnClick End Sub Sub BtnInit_OnClick TxtName.Value = "Joe Smith" TxtAddress.Value = "1 Main Street" TxtCity.Value = "Springfield" TxtState.Value = "Washington" TxtZip.Value = "12345" TxtDate.Value = Date + 3 End Sub
When the user clicks the Submit button, VBScript code is used again to validate the input. In the function BtnSubmit_OnClick(), each text field is checked for valid entry. The function CheckSpecified() checks for non-null entries and that the delivery date is reasonable. If it's not, a message box displays this message: "Not even we can deliver that fast!"
Sub BtnSubmit_OnClick bValidOrder = True Call CheckSpecified(txtName.Value, "Please specify a name.") Call CheckSpecified(txtAddress.Value, "Please specify an address.") Call CheckSpecified(txtCity.Value, "Please specify a city.") Call CheckSpecified(txtState.Value, "Please specify a state.") Call CheckSpecified(txtZip.Value, "Please specify a zip code.") Call CheckSpecified(txtDate.Value, "Please specify a date.") Call ValidateDeliveryDate If bValidOrder Then MsgBox "Thank you for your order!", 0, strMsgBoxTitle ' TODO: Actually send the order. End If End Sub Sub ValidateDeliveryDate Dim SoonestWeCanDeliver Dim RequestedDate If Not bValidOrder Then Exit Sub SoonestWeCanDeliver = Date + 2 RequestedDate = CDate(TxtDate.Value) If RequestedDate < SoonestWeCanDeliver Then bValidOrder = False MsgBox "Not even we can deliver that fast!", 0, strMsgBoxTitle End If End Sub Sub CheckSpecified(ByVal strFieldValue, ByVal strMsg) If strFieldValue = "" And bValidOrder Then MsgBox strMsg, 0, strMsgBoxTitle bValidOrder = False End If End Sub Sub BtnClear_OnClick TxtName.Value = "" TxtAddress.Value = "" TxtCity.Value = "" TxtState.Value = "" TxtZip.Value = "" TxtDate.Value = "" End Sub --> </SCRIPT>
This example shows you how easy it is to save values across Web pages using cookies. Users can click buttons to read a variable, save a variable, remove a variable, read a cookie, and flip Web pages. To use this example, go ahead and click the Save Variable Button. The following VBScript is run:
Sub SetVariable(strVariableName, varVariableValue) Document.Cookie = strVariableName & "=" & varVariableValue End Sub
I am creative, so I picked test as the variable and 1 as the value. Then I clicked the Read Variable button to see that the right variable was set. It was. Then I checked to see if the value was actually saved across Web pages by clicking the Next Page button and clicking Read Variable on that page. The following VBScript reads the variable I entered on the previous page:
Function ReadVariable(strVariableName) 'these five variables are used in the string manipulation 'code that finds the variable in the cookie. Dim intLocation Dim intNameLength Dim intValueLength Dim intNextSemicolon Dim strTemp 'calculate length and location of variable name intNameLength = Len(strVariableName) intLocation = Instr(Document.Cookie, strVariableName) 'check for existence of variable name If intLocation = 0 Then 'variable not found, so it can't be read ReadVariable = NOT_FOUND Else 'get a smaller substring to work with strTemp = Right(Document.Cookie, Len(Document.Cookie) - intLocation + 1) 'check to make sure we found the full string, not just a substring If Mid(strTemp, intNameLength + 1, 1) <> "=" Then 'oops, only found substring, not good enough ReadVariable = NOT_FOUND 'note that this will incorrectly give a not found result if and only if 'a search for a variable whose name is a substring of a preceding 'variable is undertaken. For example, this will fail: ' 'search for: MyVar 'cookie contains: MyVariable=2;MyVar=1 Else 'found full string intNextSemicolon = Instr(strTemp, ";") 'if not found, then we need the last element of the cookie If intNextSemicolon = 0 Then intNextSemicolon = Len(strTemp) + 1 'check for empty variable (Var1=;) If intNextSemicolon = (intNameLength + 2) Then 'variable is empty ReadVariable = "" Else 'calculate value normally intValueLength = intNextSemicolon - intNameLength - 2 ReadVariable = Mid(strTemp, intNameLength + 2, intValueLength) End If End If End if End Function
Links For further details please check
![]() |
VBScript Web site |
![]() |
VBScript FAQ |