学习总结之——VBScript
上一篇 / 下一篇 2008-08-04 11:41:57 / 个人分类:学习总结
1、How to Put VBscrīpt Code in an HTML Document
eg:
<html> <head> </head> <body>
<scrīpt type="text/vbscrīpt"> document.write("Hello from VBscrīpt!") </scrīpt>
</body> </html>
To insert a scrīpt in an HTML document, use the <scrīpt> tag.
Use the type attribute to define the scrīpting language.
eg:<scrīpt type="text/vbscrīpt">
Then comes the VBscrīpt: The command for writing some text
on a page is document.write:
eg:<document.write("Hello from VBscrīpt!")>
The scrīpt ends;
eg:</scrīpt>
2、VBscrīpt Procedures
We have two kinds of procedures: The Sub procedure and
the Function procedure.
A Sub procedure:
- is a series of statements, enclosed by the Sub and End Sub statements
- can perform actions, but does not return a value
- can take arguments that are passed to it by a calling procedure
- without arguments, must include an empty set of parentheses ()
eg:
Sub mysub() some statements End Sub
or
Sub mysub(argument1,argument2) some statements End Sub
A Function procedure:
- is a series of statements, enclosed by the Function
and End Function statements - can perform actions and can return a value
- can take arguments that are passed to it
by a calling procedure - without arguments, must include an empty set of parentheses ()
- returns a value by assigning a value to its name
eg:
Function myfunction() some statements myfunction=some value End Function
or
Function myfunction(argument1,argument2) some statements myfunction=some value End Function
3、Call a Sub or Function Procedure
When you call a Function in your code,you do like this:
eg:name=findname()
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:
eg:msgbox "You name is "& findname
Here you also call a Function called "findname",the Function
returns a value that will be display in the message box.
When you call a Sub procedure you can use the Call statement,
like this:
eg:Call MyProc(argument)
Or,you can omit the call statement,like this:
eg:MyProc argument
4、Conditional Statements
If ...Then...Else
You should use the If...Then...Else statement if you want to
- execute some code if a condition is true
- select one of two blocks of code to execute
If you want to execute only one statement when a condition is true,
you can write the code on one line:
eg: if i=10 then msgbox "Hello"
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":
eg:
if i=10 Then
msgbox "Hello"
i=i+1
end if
There is no..else..in this syntax either.You jiust tell the code
5、Looping Statements
For...Next Loop
You 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:
eg: For i=1 to 10 some code Next
The For Statement specifies the counter variable(i)and its start
and end values.The Next statement increaes the counter variable(i)
by one.
Step Keyword
Using 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
eg: For i=2 to 10 step 2
some code
next
To decrease the counter variableyou must use a negative step value.
You must specify an end value that is less than zhe start value.
In the example below,the counter variable(i) is decrease by two
each time the loop repeats.
eg: For i=10 to 2 step -2
some code
Next
Exit a For...Next
You can exit a For...Next statement with the Exit for keywords.
For Each...Next Loop
A For Each...Next loop repeats a block of code for each item in a
collection, or for each element of an array.
eg:dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW" For Each x in cars
document.write(x & "<br />")
Next
Do...Loop
You 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 True
You use the While keyword to check a condition
in a Do...Loop statement.
eg:
Do While i>10some codeLoopIf i equals 9, the code inside the loop above will never be executed.
eg:Do some code
Loop While i>10
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 True
You use the Until keyword to check a condition in a Do...Loop
statement.
eg:Do Until i=10
some code
Loop
If i equals 10, the code inside the loop will never be executed.
eg:Dosome codeLoop Until i=10The code inside this loop will be executed at least one time,
even if i is equal to 10.
Exit a Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword.
eg:Do Until i=10i=i-1If i<10 Then Exit DoLoopThe code inside this loop will be executed as long as i is
different from 10, and as long as i is greater than 10.
TAG: 学习总结