Examples
Example
1: Concatenation (Text addition)
Concatenation
is when you add to strings together. Look at
"Hello "&"World"="Hello World".
Notice how I got the space between the two words?, I included a space
at the end of the first Hello. In between quotation marks spaces count.
For our first example we will type in a string in the first text box,
then a second string in a second box, then add the two in the third
box.
Here
is the code:
<SCRIPT LANGUAGE="VBScript">
Sub MyButton_OnClick :REM
Run the sub routine when the button is clicked
Document.frmMyForm.txtText3.Value= _ :REM
Refer to the 3rd text box. It equals....
Document.frmMyForm.txtText1.Value& _ :REM
Refer to the 1st text box and add it to
Document.frmMyForm.txtText2.Value :REM
the second text box.
End Sub
</SCRIPT>
<FORM NAME="frmMyForm" >
<INPUT TYPE="text" NAME="txtText1" VALUE="Textbox1">
<INPUT TYPE="text" NAME="txtText2" VALUE="Textbox2">
<INPUT TYPE="text" NAME="txtText3" VALUE="answer">
<br>
<br>
<INPUT TYPE="button" NAME="MyButton" VALUE="Concatenate!">
</FORM>
Example
2: Message Boxes
Message
Boxes are handy to alert the user and for good or bad they give that
"Windows feel" to the Internet. Message Boxes like most everything
else in VBS relies on the IE DOM for its power. The concept behind the
following code is to reference the object that holds the first text
box. Then use this reference in the print method. .
Example2:
Here's the
code:
<SCRIPT LANGUAGE="VBScript">
Sub myButton2_OnClick :REM When
the user clicks the button run the sub routine
DIM message :REM
Declare the variable in memory
message = "Good bye cruel world" :REM
Assign the variable a value
Call doAlertMessage(message) :REM
Call the sub routine that does the work
End Sub
Sub doAlertMessage(Msg) :REM
This is the sub routine that does the work
Alert Msg :REM
This is the command that sends the alert to the screen
End Sub
</SCRIPT>
<FORM NAME="msgForm" >
<INPUT TYPE="button" NAME="myButton2" VALUE="click
me">
</FORM>
Example 3: The Web Calculator
The web calculator is an example of cell reference, mathmatical operators and assignment opperators as well as others.
<SCRIPT LANGUAGE="VBS">
<!--Hide from non=vbs browsers
DIM answer
Sub divBtn_OnClick()
//These routines call the values of the two input boxes and carry out the relevent action to the third box.
answer = CInt(Document.VBScalc.impOne.Value) / CInt _(Document.VBScalc.impTwo.Value)
Document.VBScalc.output1.Value = answer
End Sub
Sub mulBtn_OnClick()
answer = Cint(Document.VBScalc.impOne.Value) * CInt _(Document.VBScalc.impTwo.Value)
Document.VBScalc.output1.Value = answer
End Sub
Sub addBtn_OnClick()
answer = Cint(Document.VBScalc.impOne.Value) + CInt _(Document.VBScalc.impTwo.Value)
Document.VBScalc.output1.Value = answer
End Sub
Sub subBtn_OnClick()
answer = Cint(Document.VBScalc.impOne.Value) - CInt _(Document.VBScalc.impTwo.Value)
Document.VBScalc.output1.Value = answer
End Sub
//Stop hiding-->
//This is the form that is viewable to the user.
<FORM NAME="VBSCalc">
<DIV ALIGN="CENTER">
<INPUT TYPE="TEXT" NAME="impOne"><BR>
<INPUT TYPE="TEXT" NAME="impTwo"><BR>
<INPUT TYPE="button" NAME="divBtn" VALUE="/"> <INPUT TYPE="button" NAME="mulBtn" VALUE="*"> <INPUT TYPE="button" NAME="addBtn" VALUE="+"> >INPUT TYPE="button" NAME="subBtn" VALUE="-"><BR>
</DIV>
<INPUT TYPE="TEXT" NAME="output1">
</FORM>