VBScript Tutorial - functionx.com
Short Description
Content downloaded from www.functionx.com Created by www.ebooktutorials.blogspot.in...
Description
Content Downloaded From www.functionx.com
These are a few lessons serving as fundamentals to scripting and computer language programming. These lessons can serve as a prerequisite to Windows Script Hosting, ASP, ASP.NET, Visual Basic, or other scripting languages. Lessons
Introduction to VBScript Scripts Fundamentals Forms and Web Controls VBScript Variables VBScript operations Procedures and Functions Built-In Functions Conditional Statements Counting and Looping
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Introduction to VBScript The VBScript Computer Language Introduction The Internet has changed and improved in the past few years. From the simple static pages that webmasters used to create with the only goal of making some documents available to potential visitors of their sites, to the full blown and unique platform that the web has become. The tags of the HTML language, however diversified, were not enough to display sophisticated types of documents. Based on this, new languages were developed to respond to new needs. For its part, Microsoft decided to transform one of its existing languages for use on the Internet. The original language was Basic, which derived in Visual Basic. Microsoft therefore created a scripting language adapted for the web but which has its roots in Visual Basic. The language was named Visual Basic Scripting Edition, or VBScript. That's the language we are going to learn on this site. VBScript is very popular in the world of Microsoft platforms because it appears easy to those who have programmed in Microsoft Visual Basic.
VBScript Requirements and Lessons Prerequisites VBScript is embedded in the Microsoft Internet Explorer browser. Therefore, to use this tutorial (to learn from the lessons on this site), you will need: A computer that runs a Microsoft Windows operating system. I should mention that you will need Internet Explorer but since Windows 95, I have never seen a version of Microsoft Windows that doesn't ship with Internet Explorer. This means that, at the time of this writing, it is included in Microsoft Windows 95, 98, Me, NT, 2000, XP, Windows Server 2003. For this site, I will be using Internet Explorer 6. If you have an older version of Internet Explorer, you can freely download an updated version from the Microsoft web site. Therefore, I assume that, as long as you are using a Microsoft Windows operating system, you already have the browser you need. A text editor. Notepad is a wonderful text editor and it ships with every version of Microsoft Windows operating system. For this tutorial, I will exclusively use Notepad and all my instructions will be for Notepad. If you have another text editor or if you developed your own, feel free to use it. This tutorial assumes that you already know HTML, which you can also learn here here.. If you are learning VBScript with the goal of creating Active Server Pages (ASP), you can use any of the major browsers. This means that you can create files that have VBScript scripts with a file extension of .asp. You can view such files in Netscape or Opera.
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Learning VBScript Introduction This site assumes that you know the basics of creating folders or directories, creating and saving a file to the appropriate folder or directory. You should also know all the basic operations of opening, using and closing an application. Sometimes in the tutorials, you will be asked to perform perform some of these actions. actions. There are various ways you can create a folder in Microsoft Windows using My Computer, Windows Explorer, or an application such as Notepad. In the My Computer or Windows Explorer, you can first select the drive or an existing folder in which you want to create a new folder or file. Then, on the right side (My Computer) or in the right frame (Windows Explorer), right-click right-click and position your mouse on New: To create a folder, click Folder, type a name for the folder, and press Enter. To create a file, click the type of file from the list, type a name an press Enter. For the following exercise, we will create a file and a folder using Notepad.
Practical Learning: Using Folders and Files 1. To start Notepad, Notepad, from the task bar, click Start -> (All) Programs Programs -> Accessories -> Notepad. 2. In the empty empty file of Notepad Notepad,, type VBScript Lessons 3. To create create a folder, on the main menu, click File File -> Save. 4. In the Save As dialog dialog box, click the arrow arrow of the Save In combo box and select select the C: drive 5. Click the the Create New Folder Folder button button 6. Type VBScript Lessons Lessons and and press Enter 7. In the list of folders, make make sure you can can see vbslessons
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
8. Click Cancel 9. To save the file, on the main menu of Notepad, Notepad, click File File -> Save 10. In the Save As dialog dialog box, click the arrow arrow of the Save In combo box and select select the C: drive. 11. Find and double-click VBScript Lessons to display it in the Save In combo box. 12. Click the the arrow of the Save As Type combo combo box and select All All Files 13. Click the the File Name box and and type exercise1.htm
14. Click Save
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Previewing a File To preview one of the files on this tutorial, you will need a browser such as Netscape, Internet Explorer, or Mozilla Firefox. To preview an HTML file, you can display its folder in the Home Directory, My Computer, or Windows Explorer. After displaying the folder, you can double-click the file. If the browser browser is already already opened, opened, on the main menu, you can click File -> Open. Locate the folder or directory where the file was saved and select the HTML file you want to preview.
Practical Learning: Previewing an HTML file 1. Open Window Windows s Explorer Explorer 2. Locate the JavaScript Lessons folder where you saved the exercise1.htm file to display it 3. Double-click Double-click the file file
4. A browser should open with the file. After viewing the the file, close the browser.
Copyright © 2004-2010 FunctionX, FunctionX, Inc.
Next
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Scripts Fundamentals The Script and the Document The SCRIPT Tag One of the most usual ways you will use VBScript is to display text, as if you were using HTML. Indeed, added just a few instructions, you can use any of the HTML tags and make them part of your script. Therefore, everything you have learned in HTML is valid here. To set the instructions of a script from the HTML tags, the section that has the script must start with the tag tag and end with the tag, tag, as follows: You can write the tag in all uppercase, all lowercase, or a mix Because a script is written in a particular language that is not HTML, and because there are various scripting languages in the industry, to use a script, you should let the browser know what (particular) scripting language you are using. To let the browser know, inside of the tag, type the word Language, Language , followed by the = sign, followed by the name of the script language included in double-quotes. For example, to use a VBScript in a page, start the section with language="VBScript"> and end it with the closing tag. Therefore the scripting section can be delimited with: "" ) When the browser receives this script, it gets rid of everything that is not part of the string. After this operation, the remaining text becomes: Boo ook k Ti t l e: VBScri pt Pr og ogrr ammi ng ng< This is what the browser would try to display. As it happens, there is an HTML tag in the code, namely the break tag ; ; therefore, the break tag would be applied. You can include any HTML tag as part of your script. Make sure you use the HTML tags appropriately; VBScript will not correct or interpret your tags, it would send them to the
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com browser as they are written.
Practical Learning: Including HTML Tags in a Script 1. Change Change the file as follows: follows: Docu ocum men entt . Wr i t e( " Lea Leavi vi ng Sy Sydn dney ey " ) Document . Wr i t e( " When we deci ded t o l eave, we knew we wer e " ) Documen entt . Wr i t e( " maki ng a har har d deci deci si on on.. We had had spen spentt so much " ) Documen entt . Wr i t e( " t i me t hi s ha had d become our our ne new w ho hom me. A f ew weeks " ) Documen entt . Wr i t e( " or mon ontt hs bef or e, we wan antt ed t o make Sydne Sydney y ") Docu ocum men entt . Wr i t e( " ou ourr ne new wl y f ou ound nd set t l emen entt , a per per man anen entt pl ace " ) Docu cum ment . Wr i t e( "w "we e cou coul d pr pr oudl y cal cal l our s. I t appear ed t hat , ") Docu cum men entt . Wr i t e( "un "unp pr edi ct ab abll y, f ate had had de deci ded ot her wi se. " ) 2. Save the file and preview preview the page in your browser browser 3. After previewing previewing the page, return to Notepad 4. To make the page more attractive, attractive, change it as as follows: Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " Aut ho horr : Ar t hu hurr D. Pa Pall e br>"" ) Docu cum ment . Wr i t e( "Ti t l e: St or i es Of Of My Li f e") Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) 7. Save the file file and check it in the browser browser
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
8. After previewing previewing the page, return to Notepad and and close it.
Techniques of Managing Code HTML Comments In some cases, when an older browser interprets your code, it may not be able to validate it. To hide your code from an older browser, include it between . A comment is a line of code that is not examined, read, or interpreted by the browser. This means that the browser doesn't display anything that is part of a comment. Therefore, a comment can be written in plain English or the language you are using, with words aligned however you want them. To add a comment to your HTML code, you can include it between .
BScript Comments There are two ways you can write a line of comment in VBScript. To create a comment, start the section with the keyword Rem. Rem. Everything on the right side of Rem Rem is part of the comment. Here is an example:
Rem Thi s l i ne i s i gnor ed. I ca can n wr i t e i t howeve verr I want . Docu ocum men entt . Wr i t e( " 20 2006 06 FI FA Wor l d Cup") Cup") Using the Rem Rem keyword, you can add as many lines of comments as you want. A comment doesn't have to stand on its own line. It can be part of a line of code as long as it starts with Rem Rem and the Rem Rem keyword is not part of code. To add a comment to a line that contains code already, type Rem Rem and include your comment. Once again, anything on the right side of Rem would Rem would be ignored by the browser. Here is an example: Rem Thi s l i ne i s i gnor ed. I ca can n wr i t e i t howeve verr I want . Doc ocu umen entt . Wr i t e( " 2002 FI FA Wor l d Cu Cup") Rem The 2002 200 2 Wor l d Cup Cup was i n Rep Rep Kor Kor ea and J apa apan n Besides using the Rem Rem keyword, another technique for commenting is by using the single quote character. The single quote plays the exact same role as the Rem Rem keyword. Because of its briefness, the single quote is preferred and is more popular than the Rem keyword. Rem keyword. To create a comment, type the single quote character followed by the desired text of comment. Here is an example:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com ' Anythi ng on on t hi s si de i s par par t of a co com mment Docu ocum men entt . Wr i t e(" Th They ey scor ed t wo sof t go goal al s" ) You can use the single quote the same way you would use the Rem Rem keyword. To add a comment on a line that already has code, type ' followed by the desired text. You can also use a mix of Rem and Rem and single quote comments in the same script. Here is an example: ' Movie Quote
Docu ocum men entt . Wr i t e( " We wen entt t hr ou ough gh a l ot of t r ou oubl bl e becau because se of you you.. " ) Documen entt . Wr i t e( " You owe us. " ) Rem Max starts having a stroke here Rem Display the movie title
Docu ocum men entt . Wr i t e( " Ti t l e: Di sor ga gani ni zed Cr i me")
Practical Learning: Using Comments to VBScript Code 1. To add Rem comments Rem comments to your code, change the file as follows:
Rem This exercise illustrates the use of HTML tags inserted into VBScript code Rem This is how to add a simple table made of one row and one column
Documen entt . Wr i t e( " 0>" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Document . Wr i t e( " " ) Document . Wr i t e( " Leavi ng Sydney< Sydney" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Rem This is another example of a table
Documen entt . Wr i Docu ocum men entt . Wr i Document . Wr i Document . Wr i Documen entt . Wr i Documen entt . Wr i Document . Wr i Docu ocum men entt . Wr i Docu cum ment . Wr i Docu ocum men entt . Wr i Documen entt . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i Docu cum ment . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i Docu ocum men entt . Wr i
t e( " 0>" ) t e( " " ) t e( " " ) t e( " When we deci ded t o l eave, we knew we wer wer e " ) t e( " maki ng a har har d deci deci si on on.. We had had spen spentt so much ") t e( " t i me t hi s ha had d become our our ne new w ho hom me. A f ew wee eeks ks " ) t e( " or mont hs bef or e, we want ed t o make Sydn Sydney< ey " ) t e( " ou ourr newl y f ou ound nd set t l emen entt , a per per man anen entt pl ace " ) t e( "w "we e cou coul d pr pr oudl y cal cal l our s. I t appear ed t hat , ") t e( " un unpred predii ct ab abll y, f at e had had deci deci ded ot ot he herr wi se. " ) t e( " >" ) t e( " " ) t e( " " ) t e( " " ) t e( " " ) t e( " Aut ho horr : Ar t hu hurr D. Pa Pall e br>"" ) t e( "Ti t l e: St or i es Of Of My Li f e") t e( " " ) t e( " " ) t e( " " )
2. Save the sydney.htm file and preview the web page in your browser browser 3. Notice that that the page still still displays the same way. After previewing the page, return to Notepad 4. To use a mix of Rem and Rem and single quote comments, change the file as follows: Rem Thi s exe exer ci se i l l ust r at es t he use of HTML t ags i nse serr t ed i nt o VBScr i pt cod code e Rem Th Thii s i s how t o add add a si mpl e t ab abll e mad ade e of on one e r ow an and d one one col umn Documen entt . Wr i t e( " 0>" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Document . Wr i t e( " " ) Document . Wr i t e( " Leavi ng Sydney< Sydney" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Rem Thi s i s ano anot her ex exam ampl e of of a t abl e ' This time, the table is made of two rows and one column
Documen entt . Wr i t e( " 0>" ) Docu ocum men entt . Wr i t e( " " ) Document . Wr i t e( " " ) ' This paragraph displays in the top cell
Document . Wr i t e( " When we deci ded t o l eave, we knew we wer wer e " )
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Documen entt . Wr i t e( " maki ng a har har d deci deci si on on.. We had had spen spentt so much ") Documen entt . Wr i t e( " t i me t hi s ha had d become our our ne new w ho hom me. A f ew wee eeks ks " ) Document . Wr i t e( " or mont hs bef or e, we want ed t o make Sydn Sydney< ey " ) Docu ocum men entt . Wr i t e( " ou ourr newl y f ou ound nd set t l emen entt , a per per man anen entt pl ace " ) Docu cum ment . Wr i t e( "w "we e cou coul d pr pr oudl y cal cal l our s. I t appear ed t hat , ") Docu ocum men entt . Wr i t e( " un unpred predii ct ab abll y, f at e had had deci deci ded ot he herr wi se. " ) Documen entt . Wr i t e( " >" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " Aut ho horr : Ar t hu hurr D. Pa Pall e br>"" ) Docu cum ment . Wr i t e( "Ti t l e: St or i es Of Of My Li f e") Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) 5. Save and preview preview the file, then return return to Notepad
Code Indentation Code confined and heavily written can be hard to read. This will become even more difficult when we start adding variables, functions, and conditional statements. One remedy to making the code easier to read is to indent its sections. Although most (commercial) code editors assist programmers with code indentation, in Notepad, you will indent your code the way you want. Indentation is similar to adding empty characters to a line of text by pressing Tab when starting a line of code. It can also be performed by press the Space bar at the beginning of a line of code. This leaves an empty space or empty spaces on the left side of the line being indented. Because indentation is not part of your code, it is a matter of choice and taste. One of the most common techniques is to indent a section by two or four empty characters from the previous section. To do this, in Notepad, press the Space bar two or four times and add your code. Here is an example: Docu ocum men entt . Wr i t e( " We wen entt t hr ou ough gh a l ot of t r ou oubl bl e becau because se of you you.. " ) As you can see from the previous code, indentation of the Document.Write() calling allows you to know where the tag starts. Therefore, you will be able to know where a piece of code, such as the tag, tag, must be closed. Since your code will usually be made of different sections, a particular section of code should have the same level of indentation. Once again, this makes your code easier to read and navigate: ' Movi e Quo uott e Docu ocum men entt . Wr i t e( " We wen entt t hr ou ough gh a l ot of t r ou oubl bl e becau because se of you you.. " ) Docu ocum men entt . Wr i t e( " Yo You u owe us. " ) Rem Max st ar t s havi havi ng a st r oke he herr e Rem Di sp spll ay t he movi e t i t l e Docu ocum men entt . Wr i t e( " Ti t l e: Di sor ga gani ni zed Cr i me")
Practical Learning: Indenting Code 1. To add indentation indentation to your code, change change the file as follows: Rem Thi s exe exerr ci se i l l ust r at es the use of HTML tags tags i nse serr t ed i nt o VBScr i pt code Rem Th Thii s i s how t o add add a si mpl e t ab abll e mad ade e of on one e r ow an and d one one col col umn Documen entt . Wr i t e( " 0>" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Document . Wr i t e( " " ) Document . Wr i t e( " Leavi ng Sydn Sydney< ey" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Rem Thi s i s an anot her exam exampl e of a t abl e ' Th Thii s t i me, t he tabl e i s mad ade e of of t wo rows and and on one e col col umn Documen entt . Wr i t e( " 0>" ) Docu ocum men entt . Wr i t e( " " ) Document . Wr i t e( " " )
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com ' Thi s pa par agr aph di sp spll ays i n t he t op ce cell l Document . Wr i t e( " When we deci ded t o l eave, we knew we wer e " ) Docu ocum men entt . Wr i t e( " maki ng a har har d deci deci si on on.. We had had spen spentt so much ") Documen entt . Wr i t e( " t i me t hi s ha had d becom become our our ne new w ho hom me. A f ew weeks " ) Document . Wr i t e( " or mont hs bef or e, we want ed t o make Sydn Sydney< ey " ) Docu ocum men entt . Wr i t e( " ou ourr newl y f ou ound nd set t l emen entt , a perm perman anen entt pl ace ") Docu cum ment . Wr i t e( "w "we e co cou ul d pr pr oudl y ca cal l our s. I t appear ed t hat , ") Docu ocum men entt . Wr i t e( " un unpred predii ct ab abll y, f at e ha had d deci ded otherw otherwii se. " ) Document . Wr i t e( " >" ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " Aut ho horr : Ar t hur D. D. Pa Pall e br>"" ) Docu cum ment . Wr i t e( "Ti t l e: St or i es Of My Li f e") Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) Docu ocum men entt . Wr i t e( " " ) 2. Save and preview preview the file, then return return to Notepad
Previous
Copyright © 2004-2010 FunctionX FunctionX,, Inc.
Created by www.ebooktutorials.blogspot.in
Next
Content Downloaded From www.functionx.com
The Form and its Web Controls Overview of Web Forms Introduction A form is a section of a web page that contains one or a group of objects that the user interacts with. These objects are referred to as Windows controls, web controls, or simply controls. When using a control, the user may enter or select some values and send the result to a specific file as designed by an application developer. To support this interaction, HTML provides different text-based and selection controls that can handle various types of scenarios. Besides these, there are action controls that actually send a signal to a script. There are various types of controls you can use and different ways of creating them. Still, most controls are created using a non-closing tag called . .
The Attribute of a Control To create a control, you may start with the tag. Inside of the tag, you specify the type of control you want. We will review attributes available and what they produce.
The Events of a Control We mentioned that a user can interact with a web page using the controls on it. To do this, the user can click a control, type in it, move the caret from it to another control, etc. Everyone of these actions creates a message that must be sent to a program used by the programmer: the interpreter. The interpreter then analyzes the message and produces a result. When a message created created by a control is sent, it is also said that an event was fired. There are various types of events sent by different controls. We will mention each when necessary. When you create a web-based application, you decide what controls are necessary for your application. When you select a control, you must also know what events that control can send. In most cases, if you don't care about an event, when it occurs, the computer may ignore the event or produce a default behavior. If you think that a certain event is important for your application and that you must take action when that event fires, you should write code to react appropriately.
The Form Introduction A form is the central object that manages the other controls. Although the controls can send their data to a script, a form can be used to collect the values typed or selected on the controls, gather them as if they constituted one control and make these values available to a validating file on a server.
Creating a Form To create a form, you use the tag. tag. Because a form is a collection of controls and their values, the form must have an end that lets the browser know where the form closes. This is done with the closing tag as follows: Everything between the and the tags belong to the form and is called the body of the form. Almost anything can go in the body of the form. You can design it using any HTML tag and make it as attractive as you wish.
Forms Characteristics Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Although the and the tags are enough to create a form, such a form can hardly communicate with a script. One of the most important characteristics you should set for a form is its name. The name allows allows a script to refer to the form and it can be used by files on the server. To set the name of a form, assign an appropriate string to the Name Name attribute of the tag. tag. To refer to a form in a script, type the document keyword, document keyword, followed by a period (the period is an operator), followed by the name of the form. Suppose you create a form called CreditApplication. If you want to call it from a script, you would use: docu cum ment . Cr ed edii t Appl i ca catt i on
Practical Learning: Introducing Forms 1. Start your text editor and type type the following: Empl oyment Appl i cat i on 8"> 80">Last Last Name: Ful l Name: To retrieve the content of a text box, you can use the value value attribute. To access it, you can type the name of the form that contains the text box, followed by a period, followed by the name of the text box, followed by a period, and followed by value. value. If you plan to use a text box in a script or code of any kind, give the control a name. This is done by assigning assigning a string string to the name attribute. name attribute. Here is an example:
Events of a Text Box To use a text box, the use can click it or continuously press Tab until the caret is positioned in the text box. When this happens, the text box is said to have focus. When a text box receives focus, it fires an onFocus event. onFocus event. In most applications, a text box primarily appears empty to the user who can then enter one or more characters in it. To enter a character in a text box, the user usually presses a key on the keyboard. When this happens, the text box fires an onKeyDown onKeyDown event. When the user releases the key, the control fires an onKeyUp onKeyUp event. In some cases, the user may press a key and hold it down. down. This causes the control to fire an onKeyPress event. onKeyPress event. When the user types a character in a text box or modifies its content such as putting space between two characters or deleting a character in it, the control fires an onChange event. onChange event. This event indicates that the content of the text box is not the same it was when it first displayed to the user. When the user has finished using a control and either presses tab or clicks another control, the text box fires fires an onBlur event onBlur event to indicate that it has lost focus.
Practical Learning: Using Text Boxes 1. To use text boxes, change the file as follows: Empl oyment Appl i cat i on 8"> < 80">Last Last Name: 4">< < The most visual properties of a button are its location and the text it displays. There are various ways you can set the location of a button on the page. Although HTML provides paragraph tags that can position the button, one of the best options you have is to use a table when designing your form. This will also be valid for all controls we will learn on this site.
Button Properties The text that a button displays lets the user know what the button is used for. The text on the button is sometimes called a c aption. In HTML, this caption is the Value Value attribute of the type="button"> existing tag. To provide the button's caption, set the desired string to Value. Value. Here is an example: example: 80">Last Last Name: 2. Save the file and preview preview it in your browser browser 3. Type a name in the First Name text text box. Type another name in the Last Name text box and click Evaluate. Here is an example:
4. Return to your text editor
The Submit Button Introduction To send all of the information of a form to a script, HTML provides a special button called submit. submit. Like the regular button, the submit submit button is a rectangular object that the user clicks. To create a submit button, use the tag tag but specify the type of control as submit to the type attribute. type attribute. Here is an example:
Submit
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Unlike the regular button, the submit button has a built-in caption that automatically displays its role to the user.
Characteristics of the Submit Button The default caption of the submit button is submit. submit . If this caption doesn't apply to to your scenario, you can c an change it to a more meaningful caption. To change the submit button's caption, caption, set the desired desired string to the Value attribute Value attribute of the existing type="submit"> existing tag. Here is an example: 80">Last Last Name: 80">< >
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
The height of a text area is measured by the number of visible lines of text that the text area can display. By default, the text area is configured to display two lines of text. This property is controlled by the ROWS ROWS attribute. To change or increase this number, assign an appropriate numeric value to the rows attribute. You can specify either one of the COLS COLS or the ROWS values. values. You can also specify specify both to control the dimensions dimensions of the text area. When using both attributes, there is no order of the attributes. Here is an example:
The text that is typed in the password text box is held by the value attribute. value attribute. You can use this attribute to find out what password the user typed. If you plan to retrieve the password value in a script or code of any kind, give the control a name. This is done by assigning assigning a name string to the name attribute. name attribute. Here is an example: > To make your radio buttons effective, you should always provide at least two radio buttons in a group.
Characteristics of Radio Buttons One of the most important attributes to set on the radio button is the name. This attribute should be used even if you don't plane to use the radio button in a script, since many radio buttons can use the same name, this name will be helpful in determining what particular control has been selected. Therefore, the same value (string) for the Name Name attribute can be assigned to each radio button of the same group. The checked checked attribute, as applied to the check box, can be used in two circumstances, if you want one of the buttons in the group to be selected by default, assign a true true value to its checked attribute. checked attribute. Here is an example:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Select your T-Shirt size Small Medium Large X-Large
6"> Sm Smal l &nb nbsp; sp; >Medi um 9">XX- Lar ge ge< >Cust omer Name: 20">< < 6">Dat e Lef t : 0">< Ti me Lef t : 0">< D Dat e Exp Expect ect ed ed:: 0">< 8">Ti Ti me Exp Expect ect ed ed:: 0">< 6"> < Uni t Pr i ce Qt y 46">< 66">< Su Subb- Tot al 8">Sh Shii r t s < 8">Pan Pantt s< s < 8">< >Non one< e > Women Su Suii t >Dr ess< ess > >Reg egul ul ar Ski r t > >Ski r t Wi t h Hoo ook< k > >Men en'' s Su Suii t 2P 2Pc< c > >Men en'' s Su Suii t 3P 3Pc< c > >Sweat er s< s > Si l k Shi r t >Ti e > >Coat > >J acke ackett > Swede ede< < 1">< 8">< >Non one< e > Women Su Suii t >Dr ess< ess > >Reg egul ul ar Ski r t > >Ski r t Wi t h Hoo ook< k > >Men en'' s Su Suii t 2P 2Pc< c > >Men en'' s Su Suii t 3P 3Pc< c > >Sweat er s< s > Si l k Shi r t >Ti e > >Coat > >J acke ackett > Swede ede< < 2"><
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
8">< >Non one< e > Women Su Suii t >Dr ess< ess > >Reg egul ul ar Ski r t > >Ski r t Wi t h Hoo ook< k > >Men en'' s Su Suii t 2P 2Pc< c > >Men en'' s Su Suii t 3P 3Pc< c > >Sweat er s< s > Si l k Shi r t >Ti e > >Coat > >J acke ackett > Swede ede< < 3">< 8">< >Non one< e > Women Su Suii t >Dr ess< ess > >Reg egul ul ar Ski r t > >Ski r t Wi t h Hoo ook< k > >Men en'' s Su Suii t 2P 2Pc< c > >Men en'' s Su Suii t 3P 3Pc< c > >Sweat er s< s > Si l k Shi r t >Ti e > >Coat > >J acke ackett > Swede ede< < 4">< e">< Cl ea eani ni ng To Tott al : % % Tax Amount : 2. Save the the file file as cleaningorder.htm cleaningorder.htm in in your JavaScript Lessons folder 3. Preview it in your browser
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
4. Return Return to your text text editor editor
Characteristics of a Combo Box To select a default item from the combo box, use the selected selected attribute of the tag. Here is an example:
Smal l H Hou ourr s: 4">H Hou ourr l y Sa Sall ar y: 5">< 4">W Weekl y Hou ourr s: 4">W Weekl y Sa Sall ar y: e">< 3. Save the the file file as as payroll1.htm in your VBScript Lessons folder 4. Preview the file in the browser
5. Return to your text editor
ariable Declaration To use a variable in your script, you can just type it. Here is an example:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com NumberOfStudents
With such a variable, you can perform any of the operations we will learn shortly. Once the variable has been used somewhere, the browser reserves a space in memory for it. If you use another or new name for a variable in the same script, if the new variable has a different name than a former variable, the browser would reserve a new space in memory space for it. You can continue introducing variables like that as your script needs them. Be careful not to mistakenly type the name of variable that you want to reuse in your program. For example, if you have a variable named NumberOfStudents in your script, then find out that you want to use it in an operation, if you type NbrOfStudents instead of NumberOfStudents, since both names are different, a new memory space would be reserved for NbrOfStudents because the browser would consider that NbrOfStudents is a new variable. Trying to use both variables as one would lead to unpredictable results. To avoid this confusion, VBScript provides two alternatives. Before using a variable, you can first tell the browser that you have a variable you want to use. By doing this, the browser would be a "witness" that a certain variable exists. This is (still) not a bulletproof solution but can help to improve your code. Letting the interpreter know about a variable is referred to as declaring the variable. To declare a variable, use the Dim keyword followed by a name for the variable. Here are examples of declaring variables: Fi r st Name Dim Dim Hi r edDat e Hour l ySal ar y Dim Although the Dim keyword is used to declare a variable, it can be used for any kind of variable. It can be used to declare a variable that represents a natural number (integer), a decimal number (floating-point or double-precision number), a Boolean value, or a string. If you need to use many variables, you can declare more than one on the same line. To do this, separate them with a comma. Here is an example: Di m Ful l Name, Emai l Address Di m Addr ess, Ci t y, St at e, ZI PCode Using the Dim keyword to declare a variable doesn't completely eliminate name duplication of variables. A better alternative that VBScript proposes is to impose it upon your code that no variable should be used if it has not been previously declared. Using this technique, if you reference a variable without declaring it, the browser would display an error. To impose the rule that each variable must first be declared before being used, after the opening script tag, type option explicit as follows: Opt i on Expl i ci t ' Code goes her e If you try using or accessing a variable that was not previously declared, you may receive an error.
Practical Learning: Declaring Variables 1. Declare a few variables in a script as follows: VBScr i pt Lessons
Opt i on Expl i ci t Dim Mon, Tue, Wed, Thu, Fri, Sat, Sat, Sun Dim WeeklyHours, HourlySalary, WeeklySalary
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Empl oyee Payr ol l . . . No Change 2. Save the file file
Previous
Copyright © 2004-2010 FunctionX, FunctionX, Inc.
Created by www.ebooktutorials.blogspot.in
Next
Content Downloaded From www.functionx.com
VBScript Operations Regular Operators Introduction One of the most common assignments you will hand to your scripts consists of performing operations. VBScript is equipped with various kinds of operators to respond to any type of operation you need.
Practical Learning: Introducing Variables 1. Start Notepad or your text editor 2. From what we learned in the previous lessons, type the following: VBScr i pt Lessons Di m Mon, Tue, Wed, Thu, Fri , Sat , Sat , Sun Di m Weekl yHour s, Hour l ySal ar y, Weekl ySal ary Empl oyee Payr ol l 3. Save the file as payroll2.htm in your VBScript Lessons folder and preview it in your browser 4. Return to your text editor
The Assignment = We saw that, when declaring a variable, a memory space is reserved for it. Such a space is empty until you fill it with a value. This is performed with the assignment operation. The assignment operation gives a value to a variable. It is performed using the = operator. Its syntax is: VariableName
= Value
The VariableName must be a valid variable name. It cannot be a value such as a numeric value or a (double-quoted) string. Here is an example that assigns a numeric value to a variable: Sal ar y = 12. 55 Once a variable has been declared and assigned a value, you can call the Document.Write() function to display its value. Since the variable is part of your script and not an HTML tag, it doesn't need to be included in double-quotes (the proper sentence is that, "it doesn't need to be passed as a string"; but we have not learned what it means to pass an argument ). Here is an example: Sal ar y = 12. 55 Document . Wr i t e( Sal ary) ; To improve, safeguard, and make your code more efficient, we have learned that you should declare a variable before using it. This is done using the Dim keyword. The above script could be written as follows:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Dii m Sal ar y D Sall ar y = 12 Sa 12.. 55 Docum Docu men entt . Wr i t e( Sal ary) ; The above code declares a variable before assigning it a value. You will usually perform this assignment when you want to change the value held by a variable. The first time the browser encounters a variable, you can make sure that the variable is holding a value, just as done above. Providing a starting value to a variable is referred to as initializing the variable.
Practical Learning: Using the Assignment Operator 1. To use the the assignment operator, change change the file as follows: follows: VBScr i pt Le Lesso sson ns Di m Mon, Tue, Wed, Thu, Fri , Sat , Sun Di m Week ekll yHour our s, Hou ourr l yS ySa al ar y, Weekl yS ySal al ary Mon = Docu ocum men entt . f r mPa Payr yr ol l . t xt Mon onda day. y. Val ue Tue = Docu Tue ocum men entt . f r mPa Payr yr ol l . t xt Tu Tuesd esday ay.. Val ue Wed = Documen entt . f r mPayr ol l . t xt Wed edne nesday. sday. Val ue Thu Th u = Docu ocum men entt . f r mPa Payr yr ol l . t xt Th Thu ur sda sday. y. Val ue Fri = Docu cum ment . f r mPa Payrol yrol l . t xtFr i day. Val ue Satt = Docu Sa ocum men entt . f r mPa Payr yr ol l . t xt Sa Satt urda urday. y. Val ue Sun Su n = Docu ocum men entt . f r mPa Payr yr ol l . t xt Su Sund nday. ay. Val ue Hour l yS ySal al ary = Doc ocu umen entt . f r mPa Payrol yrol l . t xt Hou ourr l yS ySal al ar y. Val ue . . . No Change 2. Save the file but but don't previe preview w it
The Addition + The addition is an operation used to add one value to another. The syntax used it: Val ue1 + Val ue2 To perform this operation, the interpreter would add the value of Value1 to that of Value2. You can use such an operation to display the result on an HTML page. Here is an example: Document . Wr i t e( 12 125 5 + 48) The addition operation as implemented in VBScript can be applied to natural numbers called integers and to floating-point numbers called float or double-precision numbers. Here is is an example that illustrates this: ' The addi t i on oper at i on appl i ed ' t o t wo nat ur al numbers: i nt eg ege er s Documen entt . Wr i t e( 12 125 5 + 48 48)) ; Documen Docum entt . Wr i t e( " " ) ' t o deci mal number s: f l oat or doubl e Documen entt . Wr i t e( 25 2540 40.. 25 + 66 662. 2. 38 38)) Documen Docum entt . Wr i t e( " " ) You can also apply this operation on variables. For example, once a variable holds a value, you can add another value to it: Salary + 0.55 You can also add the values of two variables using their names: SalaryWeek1 + SalaryWeek2 As done in the above script, you can display the addition using the Document.Write function. After the addition operation is performed, it produces a resulting value. Sometimes you will not simply need to display the value on a web page; you may want to use it in another calculation. Therefore, you can store the result in a another variable, then use the result as you see fit. The syntax used is:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Result = Value1 + Value2
Practical Learning: Performing Additions 1. To use the the addition operator, change the the script as follows: VBScr i pt Le Lesso sson ns Di m Mon, Tue, Wed, Thu, Fri , Sat , Sun Di m Week ekll yHour our s, Hou ourr l yS ySa al ar y, Weekl yS ySal al ary Mon = Docu ocum men entt . f r mPa Payr yr ol l . t xt Mon onda day. y. Val ue Tue Tu e = Docu ocum men entt . f r mPa Payr yr ol l . t xt Tu Tuesd esday ay.. Val ue Wed = Documen entt . f r mPayr ol l . t xt Wed edne nesday. sday. Val ue Thu Th u = Docu ocum men entt . f r mPa Payr yr ol l . t xt Th Thu ur sda sday. y. Val ue Fri = Docu cum ment . f r mPa Payrol yrol l . t xtFr i day. Val ue Satt = Docu Sa ocum men entt . f r mPa Payr yr ol l . t xt Sa Satt urda urday. y. Val ue Sun Su n = Docu ocum men entt . f r mPa Payr yr ol l . t xt Su Sund nday. ay. Val ue Hour l yS ySal al ary = Doc ocu umen entt . f r mPa Payrol yrol l . t xt Hou ourr l yS ySal al ar y. Val ue Week eekll yHour s = Mon + Tue + Wed + Thu + Fr i + Sat + Sun . . . No Change
2. Save the file but but don't previe preview w it
String Concatenation: & The Document.Write function we have used so far allows you to display a string on a web page. So far, we used this function various times to display different strings. VBScript provides an operator that allows you to add different strings and create a new one. This operation is performed using the string concatenation operator &. The syntax of the & operator is: Val ue1 & Val ue2 This operation is performed on strings and you can use it to display various items using the Document.Write function. Here is an example: Opt i on Expl i ci t Di m Fi r st Name, Last Name Fi r st Name = " Rog oger er " Las t Name = " Lemer r e" docum documen entt . wr i t e( FirstName & LastName) The above script would produce RogerLemerre. An alternative would have been to include space in one of the strings. The & operator can also be used like the addition operator as long as you apply it on strings. An example example would be: Ful l Name = Fi r s t Name & Las t Name The & operator can also be applied on more than two operand. This could be used to insert characters as you see fit. Here is an example: Opt i on Expl i ci t Di m Fi r st Name, Last Name Fi r st Name = " Rog oger er " Las t Name = " Lemer r e" Docu ocum men entt . Wr i t e( " Fu Full l Name: " ) Docum Documen entt . Wr i t e( FirstName & " " & LastName ) After the & operation has been performed on two variables, the operation results in a new string. You can assign this new string to another variable, as in
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Resul t = Val ue ue1 1 & Val ue ue2 2 Here is an example: Opt i on Expl i ci t Di m Fi r st Name, Last Name, Fu Full l Name Fi r st Name = " Rog oger er " Las t Name = " Lemer r e" FullName = FirstName & " " & LastName
Docu ocum men entt . Wr i t e( " Ful l
Name: " & Ful l Name)
The Subtraction The subtraction is an operation used to subtract one value from another. The syntax used it: Val ue ue1 1 - Val ue ue2 2 To perform this operation, the interpreter would subtract the value of Value1 from that of Value2. You can use such an operation to display the result on an HTML page. Here is an example: Docu ocum men entt . Wr i t e( 208. 208. 55 - 42. 42. 16 16)) The subtraction operation as implemented in VBScript can be applied to natural numbers or decimal numbers. The subtraction can also be applied on variables and/or their values. After the subtraction operation is performed, it produces a result that can be used in other expression or assigned to another variable to store it somewhere. The syntax used would be: Result = Value1 - Value2
The Multiplication * The multiplication is used to add a number to itself a certain number of times. This operation is performed using the * operator. Its syntax is: Val ue ue1 1 * Val Val ue ue2 2 This operation can be performed on numeric values as follows: Docu ocum men entt . Wr i t e( 15 15.. 55 * 24 240. 0. 25 25)) The multiplication operation can be applied to values or variables that hold valid values. After the operation is performed, it produces a result that can be displayed on a web page or stored in a variable for later retrieval. The multiplication operation as performed in values produces a result that can be used in another expression or stored in memory through a variable. The syntax used would be: Resul t = Val ue ue1 1 * Val Val ue ue2 2
Practical Learning: Using the Multiplication 1. To perform a multiplication, multiplication, change change the script as follows: VBScr i pt Le Lesso sson ns Di m Mon, Tue, Wed, Thu, Fri , Sat , Sun Di m Week ekll yHour our s, Hou ourr l yS ySa al ar y, Weekl yS ySal al ary Mon Tue Tu e Wed Thu Th u Fri Satt Sa
= = = = = =
Docu ocum men entt . f r mPa Payr yr ol l Docu ocum men entt . f r mPa Payr yr ol l Documen entt . f r mPayr ol l Docu ocum men entt . f r mPa Payr yr ol l Docu cum ment . f r mPa Payrol yrol l Docu ocum men entt . f r mPa Payr yr ol l
. t xt Mon onda day. y. Val ue . t xt Tu Tuesd esday ay.. Val ue . t xt Wed edne nesday. sday. Val ue . t xt Th Thu ur sda sday. y. Val ue . t xtFr i day. Val ue . t xt Sa Satt urda urday. y. Val ue
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Sun = Document . f r mPayr ol l . t xt Sunday. Val ue Hour l ySal ary = Document . f r mPayrol l . t xt Hour l ySal ar y. Val ue Weekl yHour s = Mon + Tue + Wed + Thu + Fr i + Sat + Sun Weekl ySal ar y = Hour l ySal ar y * Weekl yHour s . . . No Change 2. Save the file
The Integer Division: \ Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions. This is considered that you have divided the apple in in 4 divisions. Therefore, the division is used to get the fraction of one number in terms of another. Microsoft Visual Basic provides two types of results for the division operation. If you want the result of the operation to be a natural number, called an integer, use the backlash operator "\" as the divisor. Here is an example: Val ue1 \ Val ue2 This operation can be performed on two types of valid numbers, with or without decimal parts. After the operation, the result would be a natural number. The result of either operation can be assigned to another value. It can also be displayed in a control using the assignment operator: Resul t = Val ue1 \ Val ue2
The Division / The division operation is used to divide a numeric value or the value held by a variable. The division operation is performed using the forward slash. Its syntax is: Val ue1 / Val ue2 This operation can be performed on numeric values as follows: Document . Wr i t e( "Dai l y Earni ngs: " ) Document . Wr i t e( 425. 65 / 7) The division operation can be applied to values or variables that hold valid values. The division operation produces a new value through the following syntax: Result = Value1 / Value2
The Remainder: Mod The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. Imagine you have 26 kids at a football (soccer) stadium and they are about to start. You know that you need 11 kids for each team to start. If the game starts with the right amount of players, how many will seat and wait? The remainder operation is performed with keyword Mod. Here is an example: Val ue1 Mod Val ue2 The result of the operation can be used as you see fit or you can display it in a control using the assignment operator as follows: = Val ue1 Mod Val ue2
The Negation In mathematics, a number written as 10500 or 44.12 is considered a positive number. In the computer world, we referred to such a number as unsigned (because it doesn't have a sign).
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com An unsigned number has a value greater than 0. If a number is less than 0, such a number qualifies as negative. To indicate that a number is negative, you write the - sign on its left. For example -32 is called a negative number. A value or a variable is made negative by applying the - operator to its left. Here is an example: Di m Temper at ur e Temper at ur e = - 32 The negation operator is referred to as a unary operator because it applies to only one value or variable. As illustrated above, a negative number can be assigned to a variable. A variable can also be made negative by using this operator.
The Exponentiation: ^ Exponentiation is the ability to raise a number to the power of another number. This operation is expressed using the ^ operator (Shift + 6). It uses the following mathematical formula: yx In Microsoft Visual Basic (and Microsoft Access), this formula is written as: y^x and means the same thing. Either or both y a x can be values or expression, but they must carry valid values that can be evaluated. When the operation is performed, the value of y is raised to the power of x. You can display the result of such an operation in a field using the assignment operator as follows: =y^x You can also assign the operation to an expression as follows: Total = y^x
Line Continuation Although VBScript reads code as if it were on one line, there is a maximum number of characters you can display on one line of code. Notepad can hardly display any long or too long text you have in a procedure. Unless you like scrolling left and write on your screen, you can divide your code into different lines and still let VBScript consider it a single line. To cut a line of code and continue on the subsequent line, type an underscore at the end of the line you want to interrupt. Here are two examples in a sub procedure: Di m dbl Hour s
Dim dblMonday, dblTuesday, dblWednesday, dblThursday, _ dblFriday, dblSaturday, dblSunday
Di m dbl Sal ar y Di m dbl Resul t
dblHours = dblMonday + dblTuesday + dblWednesday + dblThursday + _ dblFriday + dblSaturday + dblSunday
dbl Sal ar y = t xtSal ar y dbl Resul t = dbl Hour s * dbl Sal ar y
Previous
Copyright © 2004-2010 FunctionX, Inc.
Created by www.ebooktutorials.blogspot.in
Next
Content Downloaded From www.functionx.com
Procedures and Functions Procedures Fundamentals Introduction A procedure procedure is an assignment you you ask VBScript to perform besides, or to complete, complete, the normal flow of the program. A procedure is created to work in conjunction with the controls events of a script. There are two kinds of procedures in VBScript: A sub procedure and a function. The difference lies on their behaviors but their coding coding (programming) (programming) depends of your goal. A procedure can be included in the body of an HTML but to separate the script behavior from the rest of the file, it is usually a good idea to include the procedures in the head section of the file. So far, we were not using all parts of a regular HTML file because there was no need for such a structure. structure. From now on, we will respect the normal listing of an HTML file as follows: VBSc r i pt Tu Tut or i al Thi s i s t he bod body y of our HTML f i l e. Everything you know about HTML files, their contents and sections, is completely valid here, whether you include a script or not. When adding a script, you can use the head section to "hide" the script (actually, you are not strictly hiding it). As you may know already, there can be various things (such as the or the tags) tags) in the head section of the HTML file. Where do you position the script? It doesn't matter. You can write your script before or after the tag, tag, before or after the other tags. tags. The advantage advantage of including including a script in the head section section is that it is more likely to be interpreted before the section it refers to is reached. If you have done any type of programming before, you may know that interpreters interpreters (and compilers) compilers) read a program in a top-down approach. approach. Therefore, if the browser (actually the VBScript interpreter) finds a thing in the body section but doesn't know what that thing is because it is in the bottom part of the body section, it may not interpret interpret your script accuratel accurately. y. But if the script is in the head section, section, the interpreter interpreter will have "seen" it before reaching the body section. Based on this, from now on, many of our files will look this: VBSc r i pt Tu Tut or i al Thi s i s t he bod body y of our HTML f i l e.
Sub Procedures A sub procedure is a section of code that carries an assignment but doesn't give back a result. To create a sub procedure, start the section of code with the Sub keyword Sub keyword followed by a name for the sub procedure. To differentiate the name of the sub procedure with any other regular name, it must be followed by an opening opening and closing closing parentheses. parentheses. The section section of the sub sub procedure code closes with End Sub as Sub as follows: Sub Show ShowM MeTheDough( )
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com End Sub The name of a sub procedure should follow the same rules we reviewed for the the variables, omitting the prefix: If the sub procedure performs an action that can be represented with a verb, you can use that verb to name it. Here are examples: show, display To make the name of a sub procedure stand, you should start it in uppercase. Examples are Show, Play, Dispose, Close You should use explicit names that identify the purpose of the sub procedure. If a procedure would be used as a result of another procedure or a control's event, reflect it on the name of the sub procedure. Examples would be: afterupdate, longbefore. If the name of a procedure is a combination of words, start each word in uppercase. Examples are: AfterUpdate, SayItLoud In the following example, a sub procedure named DisplayFullName is created. It retrieves fields of two text boxes (first name and last name) on a form and displays a full name as a result of combining them: Sub Di spl ayFul l Name( ) Ful l Name = Fi r st Name & " " & Las t Name End Sub As mentioned already, you can declare variables for use in your program. In the same way, you can declare variables in the procedure if you need to. These variables are declared and dealt with in the same way we learned in the regular script sections. Using declared variables, the above procedure can be written as follows: Sub Di spl ayFul l Name( ) Di m Fi r st Name, Last Name Di m Ful l Name Ful l Name = Fi r st Name & " " & Las t Name End Sub
Calling a Procedure After creating a procedure, you can call it from another procedure, function, or control's event in the body section of an HTML file. To call a simple procedure such as the earlier DisplayFullName, you can just write the name of the sub procedure. In the following example, the above DisplayFullName sub procedure is called when the user clicks the Detail section of the form: Sub Det ai l er ( ) Di spl ayFul l Name End Sub If you want the procedure to be accessed immediately as soon as the page displays, you can assign its name to the onLoad() event of the body tag.
rguments Passing an Argument To carry an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a variable, such a variable is called an argument. Another procedure might need more that one argument, thus many arguments. The number and types of arguments of a procedure depends on various factors. If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument(s). For a procedure that is taking one argument, in the parentheses of the procedure, write a name for the argument. Here is an example: Sub Cal cul at eAr ea( Radi us) Di m dbl PI Di m dbl Ar ea dbl PI = 3. 14159 dbl Ar ea = Radi us * Radi us * dbl PI
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com End Sub A procedure can take more than one argument. If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by a comma; add the second argument and subsequent arguments and close the parentheses. There is no relationship between the arguments; for example, they can be of the same type: Sub Cal cul at ePer i met er ( Lengt h, Hei ght ) Di m dbl Per i met er dbl Per i met er = 2 * ( Lengt h + Hei ght ) End Sub The arguments of your procedure can also be as varied as you need them to be. Here is an example: Sub Di spl ayGr eet i ngs( st r Ful l Name, i nt Age) Di m Sent ence Sent ence = " Hi , " & st r Ful l Name & " . You ar e " & i nt Age & " year s ol d" End Sub
Calling an Argumentative Procedure We saw already how to call a procedure that doesn't take any argument. Actually, there are various ways you can call a sub procedure. As we saw already, if a sub procedure doesn't take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument. If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the argument, in the exact order they are passed to the sub procedure, separated by a comma. Here is an example: Sub Resul t ( ) Di m dbl Hour s, dbl Sal ar y CalcAndShowSalary dblHours, dblSalary
End Sub Sub Cal cAndShowSal ar y( Hour s, Di m dbl Resul t
Sal ar y)
dbl Resul t = Hour s * Sal ary t xtResul t = dbl Resul t End Sub Alternatively, you can use the keyword Call to call a sub procedure. In this case, when calling a procedure using Call, you must include the argument(s) between the parentheses. using Call, the above procedure could call the CalcAndShowSalary as follows: Sub Resul t ( ) Di m dbl Hour s As Doubl e Di m dbl Sal ar y As Doubl e dbl Hour s = t xt Hour s dbl Sal ar y = t xtSal ar y Call CalcAndShowSalary(dblHours, dblSalary)
End Sub
Functions Creating a Function A function is an assignment that a piece of code can take care for the functionality of a database. The main difference between a sub procedure and a function procedure is that a function can return a value. A function is created like a sub procedure with a few more rules. The creation of function starts with the Function keyword and closes with End Function. Here is an example: Funct i on Fi ndFul l Name( ) End Funct i on The name of the function follows the same rules and suggestions we have reviewed for the sub procedures. To implement a function, remember that it is supposed to return a value. In the body of the
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com function, function, describe describe what it is supposed to do. to return the right value, assign assign the desired value value to the name of the function. Here is an example: Fun Fu nct i on Cal cu cull at eAr ea( Rad adii us) Cal cul at eA eArr ea = Rad adii us * Rad adii us * 3. 1415 159 9 End En d Fun Funct ct i on A function can also be as complex as performing many and various expressions in order to get a value that can be assigned to the name of the function.
Calling a Function To call a function, you have two main alternatives. If you want to use the return value of a function in an event or another function, assign the name of the function to the appropriate local variable. Make sure you include the argument(s) of the function between parentheses.
Previous
Copyright © 2002-2010 FunctionX, Inc.
Next
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Built-In Functions Introduction VBScript ships with many functions that you can use to complete your scripts. These functions are highly reliable and can tremendously reduce your work. This means that, before creating your own procedure or function, first make sure it has not been written already because if an existing function already implements the behavior you want to apply, you should such a function. Because VBScript is an interpreted language (and not a compiled language), all of the possible built-in functions are already known to the browser. Therefore, you don't need to include a file or library. MSDN library or The best place to find out what functions already exist is by consulting the MSDN web site for its documentat documentation. ion.
Conversion Functions We saw already that the users of your web pages will be presented with objects called called controls. These objects allow the user to type values or select something from a list. Anything the user types in a text-based field is primarily considered as a string. Before performing any type of operation that involves such a value, you should make sure you can identify what kind of value it is. For example, you shouldn't try to multiply a string by a date such as FirstName * January 16. Although you will not be able to avoid every single type of problem that could occur in a page, you can reduce reduce errors by checking checking the value that a control control holds. The first thing you should do with a value retrieved from a control is to convert it to the appropriate type. There are various conversion functions adapted to the different possible kinds of values. The general syntax of the conversion functions is: ReturnType = Function(Expression) The expression could be of any kind, depending on how the expression would be supplied. For example, example, it could be a string or value the user would have entered in form. It could also be the result of a calculation performed on another procedure or function. The function would take such a value, string, or expression and attempt to convert. If the conversion is successful, the function function would return return a new value that is of the type specified by the ReturnType in our syntax. The conversion functions are as follows: Function Name
Return Type
Description
C B o ol
Boolean
CByte
Byte
Converts Converts an expression expression into Byte number
CDate
Date
Converts and expression into a date or time value
CDbl
Double
Converts Converts an expression into a flowing-point (decimal) number
CInt
Integer
Converts Converts an expression into an integer (natural) number
CCur
Currency
CLng
Long
Converts Converts an expression into a long integer (a large natural) number
CSng
S i n gl e
Converts Converts an expression into a flowing-point (decimal) number
C S tr
String
Converts an expression into a string
Converts Converts an expression into a Boolean value
Converts Converts an expression into a currency (monetary) value
Practical Learning: 1. Start your your text editor. 2. In the empty file, file, type the following: following:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Employees Payroll Oddenton Auto Repair Employees Payroll First Name: LastName: Hourly Salary Weekly Hours Mon Tue Wed Thu Fri Sat Sun
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com 3. Save the the file as payroll2.htm in payroll2.htm in the vbstutorial folder vbstutorial folder and preview it in the browser:
4. After previewing previewing the the page, return to your text editor. editor. 5. To perform calculations that involve values stored in text boxes, change the file as follows: Slockum Enterprises - User Account
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com ConfPassw ConfPassword ord Then Document.frmRegistration.txtResult.Value = "Your Passwords Do Not Match" End If End Sub --> Slockum Enterprises In order to access this site, you must be a member. Please create an acount. First Name: Last Name: Username: E-Mail Address: Password: Confirm Password: Result: 7. Save and preview preview the file in the browser: browser:
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
8. After previewi previewing ng the form, return return to your text editor.
Decision Makers: The If...Then...Else Statement The If...Then statement offers only one alternative: to act if the condition is true. Whenever you would like to apply an alternate alternate expression expression in case the condition condition is false, use the If...Then...Else statement. If...Then...Else statement. The formula of this statement is: If ConditionIsTrue If ConditionIsTrue Then Expression1 Else Expression2 End If
Decision Makers: The If...Then...ElseIf Statement The If...Then...ElseIf statement acts like the If...Then...Else If...Then...Else,, except that it offers as many choices as necessary. The formula is: If Condition1 If Condition1 Then Statement1 ElseIf Condition2 ElseIf Condition2 Then Statement2 ElseIf Conditionk ElseIf Conditionk Then Then Statementk End If The program will first examine Condition1. Condition1. If Condition1 Condition1 is true, the program will execute execute Statment1 Statment1 and stop examining conditions. But if Condition1 is Condition1 is false, the program will examine Condition2 Condition2 and act accordingly. Whenever a condition is false, the program will continue examining the conditions until it finds one. Once a true condition has been found and its statement executed, the program will terminate the conditional examination at End If.
Decision Makers: The Select Case Statement If you have a large number of conditions to examine, the If...Then...Else If...Then...Else will will go through each one of them, which could take long (although usually transparent to the user). VBScript offers the alternative alternative of jumping to the statement that applies to the state of the condition. condition. The formula of the Select Case is: Case is: Select Case Expression Case Expression1 Statement1 Case Expression2
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Statement2 Case Expressionk Statementk End Select The interpreter will examine the Expression and evaluate it once. Then it will compare the result of this examination with the Expressionn of each case. Once it finds one that matches, it would execute the corresponding Statement . If you anticipate that there could be no match between the Expression and one of the Expressions, you can use a Case Else statement at the end of the list. The statement would then look like this: Select Case Expression Case Expression1 Statement1 Case Expression2 Statement2 Case Expressionk Statementk Case Else Statementk End Select
Previous
Copyright © 2002-2010 FunctionX, Inc.
Next
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com
Counting and Looping The Do...Loop Statements Loops are are used to repeat an action. action. There are various various variations variations of the Do loops. Do loops. The formula of the Do While loop is: Do While Condition Statement(s) Loop This expression will execute the Statement or or statements AS LONG AS the Condition is true, as many times as the Condition will be visited and found true. The program will first test the Condition. If the Condition is true, the program will execute the Statement or Statement s and go back to the Do While While statement and test the condition again. If the Condition is false, the program program will skip the Do While statement and not execute any. Since the Do While While statement tests the Condition first before executing the Statement , sometimes you will want the program to execute the Statement first, first, then go back and test the Condition. VBScript offers a reverse to the formula, which is: Do Statement(s) Loop While Condition In this case, VBScript will execute the Statement or Statement s first, then it will test the again. The program Condition. If the Condition is true, the program will execute the Statement again. will continue this examination-execution as long as the Condition is true. The big difference here is that even if the Condition is false, false, the program program will have executed executed the Condition at least once. An alternative alternative to the Do While loop While loop is the Do Until loop. Until loop. Its formula is: Do Until Condition Statement(s) Loop This loop will first examine the Condition, instead of examining whether the Condition is true, it will test whether whether the Condition is false. The other side of the Do Until loop will execute execute the Statement first, first, then it would examine the Condition. The formula is: Do Statement(s) Loop Until Condition
Decision Makers: The For...Next Statement If you don't know how many times a statement needs to be executed, you can use one of the Do loops. But whenever you want to control how many times a statement should be executed, Do the For...Next For...Next loop loop offers a better alternative. The formula is: For Counter = Start To End Statement(s) Next Used for counting, the For For loop begins counting at the Start point. Then it examines whether the current value (after starting to count) is greater than End ; if that's the case, the program exits the loop. It then executes the Statement or Statements . Next, it increments the value of Counter by by 1 and examines the condition again. This process goes on until Counter = End . The formula above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the formula: For Counter = Start To End Step Increment Statement(s) Next Counter You can set the incrementing value to your choice. If the value of Increment is positive, the
Created by www.ebooktutorials.blogspot.in
Content Downloaded From www.functionx.com Counter will will be added its value. This means you can give it a negative value, in which case the Counter will will be subtracted the set value.
Decision Makers: The For Each...Next Statement Since the For Next Next loop is used to execute a group of statements based on the current result of the loop counting from Start to End , an alternative is to state various steps in the loop and execute a group of statements for each one of the elements in the group. This is mostly used when dealing with a collection of items. The formula is: For Each Element In Group Statement(s) Next Element The loop will execute the Statement or or Statement (s) (s) for each Element in in the Group.
Previous
Copyright © 2002- 2010 FunctionX, FunctionX, Inc.
Created by www.ebooktutorials.blogspot.in
View more...
Comments