What is VBScript..
This section discusses VBScript, and provides a brief introduction to using it in HTML documents.
© Copyright Brian Brown, 1997. All rights reserved.
This section discusses VBScript, and provides a brief introduction to using it in HTML documents.
© Copyright Brian Brown, 1997. All rights reserved.

Visual Basic Script is a scripting language designed to enhance HTML pages and provide functionality such as data validation, forms and interaction with Active X objects. It is a subset of Visual Basic, and embedded into an HTML document and interpreted by the client browser. At this time, only Internet Explorer supports VBScript.
Inserting VBScript into HTML Documents
VBscript statements are inserted into HTML documents using the <SCRIPT> tag. These can appear anywhere in the document. Consider the following example, which writes the current date and time into the HTML document at the point where the VBScript occurs....
Example 1
The following example embeds the current date and time directly into the HTML document.
The following example embeds the current date and time directly into the HTML document.
The date is
2008-03-03
The code for this is as follows........<script language="VBScript">
<!-- start of VBscript hide from old browsers
document.write( date() )
-->
</script>
<!-- start of VBscript hide from old browsers
document.write( date() )
-->
</script>
The <SCRIPT> and </SCRIPT> tags surround the VBScript code. In addition, this is further wrapped using the embedded comment tags<!-- and -->. The embedded comment tags prevents browsers which do not support the script tag from displaying the VBScript code.
In this particular case, the opening <SCRIPT> tag specifies the language to be used as VBScript. Normally, VBScript statements are placed in the HEAD section of the HTML page, but as in the example above, can occur in the BODY section also.
Procedures
VBScript supports two kinds of procedures, SUB and FUNCTION. A procedure is a series of VBScript statements combined into a single unit and assigned a name. A procedure could validate a users entry in a dialog box, write specific details to the document, access a database or manipulate an ActiveX control.
Procedures
VBScript supports two kinds of procedures, SUB and FUNCTION. A procedure is a series of VBScript statements combined into a single unit and assigned a name. A procedure could validate a users entry in a dialog box, write specific details to the document, access a database or manipulate an ActiveX control.
Sub Procedures
A sub procedure is a series of VBScript statements enclosed by the SUB and END SUB statements that does not return a value. As such it is used for simple actions like prompting for input or displaying information. If the sub procedure does not accept arguments, its name must be followed by an empty set of parentheses.
A sub procedure is a series of VBScript statements enclosed by the SUB and END SUB statements that does not return a value. As such it is used for simple actions like prompting for input or displaying information. If the sub procedure does not accept arguments, its name must be followed by an empty set of parentheses.
Here is an example of a sub procedure which displays a text input box and when the user enters data into it, converts the entry from farenheight to celcius and displays it in a message box.
Example 2
The following example allows the user to enter in a temperature and then displays this in a message box. In further examples, this will be expanded. The VBScript code contains a function GetTemp, which uses the InputBox function and the MsgBox function to read the temperature and display it respectively. In addition, a standard <FORM> element and submit button are used to activate the VBScript procedure. Enter Number in Degrees Farenheight:
The following example allows the user to enter in a temperature and then displays this in a message box. In further examples, this will be expanded. The VBScript code contains a function GetTemp, which uses the InputBox function and the MsgBox function to read the temperature and display it respectively. In addition, a standard <FORM> element and submit button are used to activate the VBScript procedure. Enter Number in Degrees Farenheight:
The code for this is as follows........
<script language="VBScript">
<!-- start of VBscript hide from old browsers
sub CheckTemp1_OnClick()
Dim TheForm
Dim DegreesF
Set TheForm = Document.GetTemp1
DegreesF = TheForm.DegreesF.Value
MsgBox "The temperature in Farenheight is " & DegreesF & "degrees F."
End Sub
-->
</script>
<form name="GetTemp1">
Enter Number in Degrees Farenheight:
<input type="text"size="20" name="DegreesF">
<input type="button" name="CheckTemp1" value="Temperature">
</form>
<!-- start of VBscript hide from old browsers
sub CheckTemp1_OnClick()
Dim TheForm
Dim DegreesF
Set TheForm = Document.GetTemp1
DegreesF = TheForm.DegreesF.Value
MsgBox "The temperature in Farenheight is " & DegreesF & "degrees F."
End Sub
-->
</script>
<form name="GetTemp1">
Enter Number in Degrees Farenheight:
<input type="text"size="20" name="DegreesF">
<input type="button" name="CheckTemp1" value="Temperature">
</form>
A FORM is used to hold a text entry box labeled DegreesF and a push button labeled CheckTemp1. When the user clicks on this button, the associated VBScript event CheckTemp1_OnClick() is invoked. This sub procedure defines a new form variable called TheForm using the statement Dim TheForm and a new variable called DegreesF using the statement Dim DegreesF.
The statement Set TheForm = Document.GetTemp1 sets the variable TheForm to the Form element given the name GetTemp1. This allows access to the text entry box DegreesF which is done by the statement line DegreesF = TheForm.DegreesF.Value
The use of MsgBox displays the converted temperature by using concatenated strings, the & character is used to indicate concatenation and the quotes to define text strings.
Function Procedures
A function in VBScript is a series of VBScript statements which are enclosed by the FUNCTION and END FUNCTION statements. A function which does not accept any values must have the name of the function followed by an empty set of parentheses. In addition, the name of the function is somewhere assigned a value which is used as the return value for the function.
A function in VBScript is a series of VBScript statements which are enclosed by the FUNCTION and END FUNCTION statements. A function which does not accept any values must have the name of the function followed by an empty set of parentheses. In addition, the name of the function is somewhere assigned a value which is used as the return value for the function.
It would be more meaningful if we actually did something better than just echo users responses. Lets consider the previous example, and this time, expand it to convert the temperature from Farenheight to Celcuis. To do this, we will add a function ConvertCelcius() which will take the value in degrees Farenheight and return the answer in degrees Celcius.
It looks like
Enter Number in Degrees Farenheight:
And the VBScript code to implement this change looks like
<script language="VBScript">
<!-- start of VBscript hide from old browsers
sub CheckTemp2_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp2
DegreesF = TheForm.DegreesF.Value
DegreesC = GetCelcius2( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
End Sub
Function GetCelcius2( DegreesF )
GetCelcius2 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp2">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp2" value="Temperature">
</form>
<!-- start of VBscript hide from old browsers
sub CheckTemp2_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp2
DegreesF = TheForm.DegreesF.Value
DegreesC = GetCelcius2( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
End Sub
Function GetCelcius2( DegreesF )
GetCelcius2 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp2">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp2" value="Temperature">
</form>
The Function GetTemp2() also indicates the declaration of a local variable called DegreesC using the DIM statement. The statement DegreesC = GetCelcius2( DegreesF ) passes the value of DegreesF to the function GetCelcius2() and returns the result into the variable DegreesC.
Validating Numeric Entry
In this example, the scripts are expanded to ensure that the entry is numeric. Obviously, the function GetCelcius() should not be called if the entry is not numeric.
Validating Numeric Entry
In this example, the scripts are expanded to ensure that the entry is numeric. Obviously, the function GetCelcius() should not be called if the entry is not numeric.
It uses the IsNumeric() function in VBScript to determine if the field is numeric.
It looks like
Enter Number in Degrees Farenheight:
The code looks like
<script language="VBScript">
<!-- start of VBscript hide from old browsers
sub CheckTemp3_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp3
DegreesF = TheForm.DegreesF.Value
if IsNumeric( DegreesF ) Then
DegreesC = GetCelcius3( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
else
MsgBox "Please enter a numeric Value."
End If
End Sub
Function GetCelcius3( DegreesF )
GetCelcius3 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp3">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp3" value="Temperature">
</form>
<!-- start of VBscript hide from old browsers
sub CheckTemp3_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp3
DegreesF = TheForm.DegreesF.Value
if IsNumeric( DegreesF ) Then
DegreesC = GetCelcius3( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
else
MsgBox "Please enter a numeric Value."
End If
End Sub
Function GetCelcius3( DegreesF )
GetCelcius3 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp3">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp3" value="Temperature">
</form>
This demonstrates the use of anIf then Else End If statement to checkif the data entry is numeric.
Validating the Number Range
In this example, we expand the previous by including a range check on the input numeric data, in this case, we limit the input data to a range of 32 to 100 inclusive. This is done using an additional if then else end if statement and a compound relational test.
Validating the Number Range
In this example, we expand the previous by including a range check on the input numeric data, in this case, we limit the input data to a range of 32 to 100 inclusive. This is done using an additional if then else end if statement and a compound relational test.
It looks like
Enter Number in Degrees Farenheight:
The code looks like
<script language="VBScript">
<!-- start of VBscript hide from old browsers
sub CheckTemp4_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp4
DegreesF = TheForm.DegreesF.Value
if IsNumeric( DegreesF ) Then
if DegreesF < 32 Or DegreesF > 100 Then
MsgBox "Please enter a value between 32 and 100"
Else
DegreesC = GetCelcius4( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
End If
else
MsgBox "Please enter a numeric Value."
End If
End Sub
Function GetCelcius4( DegreesF )
GetCelcius4 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp4">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp4" value="Temperature">
</form>
<!-- start of VBscript hide from old browsers
sub CheckTemp4_OnClick()
Dim TheForm
Dim DegreesF
Dim DegreesC
Set TheForm = Document.GetTemp4
DegreesF = TheForm.DegreesF.Value
if IsNumeric( DegreesF ) Then
if DegreesF < 32 Or DegreesF > 100 Then
MsgBox "Please enter a value between 32 and 100"
Else
DegreesC = GetCelcius4( DegreesF )
MsgBox "The temperature in Celcius is " & DegreesC & "degrees C."
End If
else
MsgBox "Please enter a numeric Value."
End If
End Sub
Function GetCelcius4( DegreesF )
GetCelcius4 = (DegreesF - 32) * 5 / 9
End Function
-->
</script>
<form name="GetTemp4">
Enter Number in Degrees Farenheight: <input type="text" size="20" name="DegreesF">
<input type="button" name="CheckTemp4" value="Temperature">
</form>
Documentation http://www.microsoft.com/vbscript/us/techinfo/vbsdocs.htm
http://www.microsoft.com/VBScript/us/download/vbsdoc.exe

Latest V2.0 Scripting Engine for VBScript and JavaScript
http://www.microsoft.com/msdownload/scripting.htm

null
최근 덧글