VB Script – Objects with VB Script
In this tutorial you will learn about VB Script – Objects with VB Script, Introduction, How to Add Objects to your web pages, How to Add Objects to your Web Pages and How to link Vb Script with Objects.
Introduction
Objects which are in the form of ActiveX controls and java applets are useful for the enhancement of our web pages. By using a scripting languages like vbscript you can extend the capabilities of these controls by integrating in to scripts.
How to Add Objects to your web pages.
Objects are added to a web page with the < object > tag.There are some attributes to this < object > tag called < param > tags.
How to Add Objects to your Web Pages
The following below given example demonstrates how an ActiveX control might appear when added to a page.
< Object Id="lblstatus” width=54 height=20 ClassID="CLSID:978C9E23-D4BU-11CE-BF2D-00AA003F40D0” > < Param name="Forecolor” value="0” > < Param name="Backcolor” value="16777215” > < Param name="Caption” value="Example of Vb script” > < Param name="Size” value="1582;635” > < Param name="Specialeffect” value="2” > < Param name="fontheight” value="200” > < Param name="Fontcharset” value="0” > < /Object > |
How to link Vb Script with Objects
When you add any control to your webpage, it can easily be configured, manipulated through its properties, methods and events.
Now the question arises, What are the properties.
Well Properties means the characteristics of an object. Properties can include items like caption, Background color, foreground color, font size, or the size. Next, questions arises in mind is What are methods.
If you had previously studies any programming language than you might be knowing what methods are. Methods can cause an object to perform a particular task and Events are the actions that are recognized by an object.For example : Submit button recognizes an OnClick event as we have used in our previous examples.
For example :
< script language=”Vbscript” > Sub cmdCalculatesalary_onClick Dim payrate,totpay,hoursworked Hrsworked=InputBox(“Enter how many hours you have worked this week”) Payrate=InputBox(“Enter to Pay rate”) totpay=hrsworked * payrate lbltotpay.caption=totpay End Sub < /script > |
Now in the above example the caption is the property of lbltotpay and is used to set the results of our calculation.
Example :
< HTML > < HEAD > < TITLE >vbscript example< /TITLE > < SCRIPT LANGUAGE="VBScript" > < !– Add this to instruct non-IE browsers to skip over VBScript modules. Option Explicit Sub cmdCalculate_OnClick Dim AmountofTax Dim Subtotal Dim TAX_RATE Dim TotalCost ‘ Define our constant values. TAX_RATE = 0.06 ‘ Perform order calculations. Subtotal = Document.myform.txtQuantity.Value * Document.myform.txtUnitPrice.Value AmountofTax = Subtotal * TAX_RATE TotalCost = Subtotal + AmountofTax ‘ Display the results. Document.myform.lblSubtotal.Caption = Subtotal Document.myform.lblTaxes.Caption = AmountofTax Document.myform.lblTotalCost.Caption = TotalCost End Sub — > < /SCRIPT > < /HEAD > < BODY > < FORM NAME="myform" > < TABLE > < TR > < TD >< B >Quantity:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtQuantity" SIZE=5 >< /TD > < /TR > < TR > < TD >< B >Unit price:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtUnitPrice" SIZE=5 >< /TD > < /TR > < TR > < TD >< INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" >< /TD > < TD >< /TD > < /TR > < TR > < TD >< B >Subtotal:< /B >< /TD > < TD > < OBJECT ID="lblSubtotal" WIDTH=45 HEIGHT=24 CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" > < PARAM NAME="ForeColor" VALUE="0" > < PARAM NAME="BackColor" VALUE="16777215" > < PARAM NAME="Caption" VALUE="" > < PARAM NAME="Size" VALUE="1582;635" > < PARAM NAME="SpecialEffect" VALUE="2" > < PARAM NAME="FontHeight" VALUE="200" > < PARAM NAME="FontCharSet" VALUE="0" > < PARAM NAME="FontPitchAndFamily" VALUE="2" > < PARAM NAME="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < TR > < TD >< B >Taxes:< /B >< /TD > < TD > < OBJECT ID="lblTaxes" WIDTH=45 HEIGHT=24 CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" > < PARAM NAME="ForeColor" VALUE="0" > < PARAM NAME="BackColor" VALUE="16777215" > < PARAM NAME="Caption" VALUE="" > < PARAM NAME="Size" VALUE="1582;635" > < PARAM NAME="SpecialEffect" VALUE="2" > < PARAM NAME="FontHeight" VALUE="200" > < PARAM NAME="FontCharSet" VALUE="0" > < PARAM NAME="FontPitchAndFamily" VALUE="2" > < PARAM NAME="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < TR > < TD >< B >Total Cost:< /B >< /TD > < TD > < OBJECT ID="lblTotalCost" WIDTH=45 HEIGHT=24 CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0" > < PARAM NAME="ForeColor" VALUE="0" > < PARAM NAME="BackColor" VALUE="16777215" > < PARAM NAME="Caption" VALUE="" > < PARAM NAME="Size" VALUE="1582;635" > < PARAM NAME="SpecialEffect" VALUE="2" > < PARAM NAME="FontHeight" VALUE="200" > < PARAM NAME="FontCharSet" VALUE="0" > < PARAM NAME="FontPitchAndFamily" VALUE="2" > < PARAM NAME="FontWeight" VALUE="0" > < /OBJECT > < /TD > < /TR > < /TABLE > < /FORM > < /BODY > < /HTML > |
The output on the browser would be as under:
Here the script has been modified to remove the MsgBox function and in its place we have set the caption property of the three label controls.See the statements we have used in the code.
‘ Display the results.
Document.myform.lblSubtotal.Caption = Subtotal
Document.myform.lblTaxes.Caption = AmountofTax
Document.myform.lblTotalCost.Caption = TotalCost
Just understand these points and you will be able to know how to reference a textbox or control.
Document means — > Our web document
Myform means — > The form on which Active X controls are placed.
LblTaxes — > The name of the control.
Caption — > The Property to set.
Hopefully now you are able to understand how to use the above statements in your vbscripts.