|
|||||||||||
![]() ![]() |
|
||||||||||
|
Gateway Education and Welfare Society (Regd.), Reg. No. DIC/DRA/1218 of 2002-- |
|||||||||||
|
|
VBScriptVBScript is a Microsoft scripting language. In our VBScript tutorial you will learn how to write VBScript, and how to insert these scripts into your HTML files to make your web pages more dynamic and interactive. What is VBScript?
How Does it Work?When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. How to Put VBScript Code in an HTML Document
And it produces this output:
To insert a script in an HTML document, use the <script> tag. Use the type attribute to define the scripting language.
Then comes the VBScript: The command for writing some text on a page is document.write:
The script ends:
How to Handle Older BrowsersOlder browsers that do not support scripts will display the script as page content. To prevent them from doing this, you can use the HTML comment tag:
Where to Put the VBScriptScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, other times when a user triggers an event. Scripts in the head section: Scripts to be executed when they are called or when an event is triggered go in the head section. Usually we put all the "functions" in the head section.he reason for this is to be sure that the script is loaded before the function is called:
Scripts in the body section: Scripts to be executed when the page loads go in the body section. When you place a script in the body section it generates the content of the page:
Scripts in both the body and the head section: You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.
What is a Variable?A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. In VBScript, all variables are of type variant, that can store different types of data. Rules for Variable Names:
Declaring VariablesYou can declare variables with the Dim, Public or the Private statement. Like this:
Now you have created a variable. The name of the variable is "name". You can also declare variables by using its name in your script. Like this:
Now you have also created a variable. The name of the variable is "name". However, the last method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running. This is because when you misspell for example the "name" variable to "nime" the script will automatically create a new variable called "nime". To prevent your script from doing this you can use the Option Explicit statement. When you use this statement you will have to declare all your variables with the dim, public or private statement. Put the Option Explicit statement on the top of your script. Like this:
Assigning Values to VariablesYou assign a value to a variable like this:
The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "name" has the value "Hege". Lifetime of VariablesHow long a variable exists is its lifetime. Array VariablesSometimes you want to assign more than one value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. The declaration of an array variable uses parentheses ( ) following the variable name. In the following example, an array containing 3 elements is declared:
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:
Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this:
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:
VBScript ProceduresWe have two kinds of procedures: The Sub procedure and the Function procedure. A Sub procedure:
A Function procedure:
Call a Sub or Function ProcedureWhen you call a Function in your code, you do like this:
Here you call a Function called "findname", the Function returns a value that will be stored in the variable "name". Or, you can do like this:
Here you also call a Function called "findname", the Function returns a value that will be displayed in the message box. When you call a Sub procedure you can use the Call statement, like this:
Or, you can omit the Call statement, like this:
Conditional StatementsVery often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In VBScript we have four conditional statements:
If....Then.....ElseYou should use the If...Then...Else statement if you want to
If you want to execute only one statement when a condition is true, you can write the code on one line:
There is no ..else.. in this syntax. You just tell the code to perform one action if the condition is true (in this case if i=10). If you want to execute more than one statement when a condition is true, you must put each statement on separate lines and end the statement with the keyword "End If":
There is no ..else.. in this syntax either. You just tell the code to perform multiple actions if the condition is true. If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword:
The first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is not equal to 10). If....Then.....ElseifYou can use the if...then...elseif statement if you want to select one of many blocks of code to execute:
Select CaseYou can also use the SELECT statement if you want to select one of many blocks of code to execute:
This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed. Looping StatementsVery often when you write code, you want to allow the same block of code to run a number of times. You can use looping statements in your code to do this. In VBScript we have four looping statements:
For...Next LoopYou can use a For...Next statement to run a block of code, when you know how many repetitions you want. You can use a counter variable that increases or decreases with each repetition of the loop, like this:
The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one. Step KeywordUsing the Step keyword, you can increase or decrease the counter variable by the value you specify. In the example below, the counter variable (i) is increased by two each time the loop repeats.
To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. In the example below, the counter variable (i) is decreased by two each time the loop repeats.
Exit a For...NextYou can exit a For...Next statement with the Exit For keyword. For Each...Next LoopA For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.
Do...LoopYou can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code is repeated while a condition is true or until a condition becomes true. Repeating Code While a Condition is TrueYou use the While keyword to check a condition in a Do...Loop statement.
If i equals 9, the code inside the loop above will never be executed.
The code inside this loop will be executed at least one time, even if i is less than 10. Repeating Code Until a Condition Becomes TrueYou use the Until keyword to check a condition in a Do...Loop statement.
If i equals 10, the code inside the loop will never be executed.
The code inside this loop will be executed at least one time, even if i is equal to 10. Exit a Do...LoopYou can exit a Do...Loop statement with the Exit Do keyword.
The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.
|
| Admin | Home | About Society | Aims and Objects | Contact | Sitemap | |
|
Gateway Educational and Welfare Society is an NGO Regd. with Govt. Of Punjab to promote the skills of poor and needy youth of INDIA so that they may earn respectful living hood. Presently the area of functioning of Society is Sangrur Distt. |
||||||
|
Copyright to Starshine India |
||||||