ΠΑΡΑΔΕΙΓΜΑΤΑ ΣΤΗΝ VBScript

Page 1

ƁƈƁƍƁƒƉƏƒ ƅ. ƊƏƔƓƒƏƍƉƊƏƋƁƒ

ȆĮȡĮįİȓȖµĮIJĮ ıIJȘȞ

VBScript

ƮƭƧƷƳ 2004



ȆĮȡĮįİȓȖµĮIJĮ Java

ȆǼȇǿǼȋȅȂǼȃǹ 0.

ǼȚıĮȖȦȖȒ ....................................................................................................................1

1.

ȉȠ ȆȡȫIJȠ ȆȡȩȖȡĮµµĮ ıİ VBScript.............................................................................1

2.

ȂȠȡijȠʌȠȓȘıȘ ȀİȚµȑȞȠȣ µİ VBScript...........................................................................1

3.

ȈȣȞĮȡIJȒıİȚȢ ıIJȠ ȉµȒµĮ head .....................................................................................1

4.

Scripts ıIJȠ ȉµȒµĮ body...............................................................................................1

5.

ȅȚ ȂİIJĮȕȜȘIJȑȢ ıIJȘȞ VBScript......................................................................................2

6.

ȅȚ ȆȓȞĮțİȢ ıIJȘȞ VBScript ............................................................................................2

7.

ȅȚ ǻȚĮįȚțĮıȓİȢ (Procedures) ıIJȘȞ VBScript ................................................................3

8.

ȅȚ ȈȣȞĮȡIJȒıİȚȢ (Functions) ıIJȘȞ VBScript .................................................................3

9.

Ǿ ǼȞIJȠȜȒ If ..................................................................................................................4

10.

Ǿ ǼȞIJȠȜȒ Select Case .................................................................................................5

11.

Ǿ ǼȞIJȠȜȒ For ... Next...................................................................................................5

12.

Ǿ ǼȞIJȠȜȒ For Each ... Next .........................................................................................6

13.

Ǿ ǼȞIJȠȜȒ Do ... Loop...................................................................................................6

14.

ȅȚ ȈȣȞĮȡIJȒıİȚȢ UCase() țĮȚ LCase() ..........................................................................7

15.

ȅȚ ȈȣȞĮȡIJȒıİȚȢ Trim(), LTrim() țĮȚ RTrim() .................................................................7

16.

Ǿ ȈȣȞȐȡIJȘıȘ StrReverse()..........................................................................................7

17.

Ǿ ȈȣȞȐȡIJȘıȘ Round() .................................................................................................7

18.

ȅȚ ȈȣȞĮȡIJȒıİȚȢ Rnd() țĮȚ Randomize().......................................................................7

19.

Ǿ ȈȣȞȐȡIJȘıȘ Int()........................................................................................................8

20.

ȅȚ ȈȣȞĮȡIJȒıİȚȢ Left() țĮȚ Right() .................................................................................8

21.

Ǿ ȈȣȞȐȡIJȘıȘ Replace() ..............................................................................................8

22.

Ǿ ȈȣȞȐȡIJȘıȘ Mid()......................................................................................................8

23.

ȈȣȞĮȡIJȒıİȚȢ ǾµİȡȠµȘȞȓĮȢ țĮȚ ǷȡĮȢ ...........................................................................8

ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ



ȆĮȡĮįİȓȖµĮIJĮ VBScript -1-

0. ǼȚıĮȖȦȖȒ ǵʌȦȢ ȣʌȠȞȠİȓ țĮȚ IJȠ ȩȞȠµȐ IJȘȢ, Ș VBScript (Visual Basic Script) İȓȞĮȚ µȚĮ ȖȜȫııĮ ıȣȖȖȡĮijȒȢ ıİȞĮȡȓȦȞ (scripting language) ʌȠȣ ȤȡȘıȚµȠʌȠȚİȓIJĮȚ ȖȚĮ ȞĮ ʌȡȠıșȑıİȚ İijȑ țĮȚ įȚĮȜȠȖȚțȩIJȘIJĮ (interactivity) ıIJȚȢ ȚıIJȠıİȜȓįİȢ µĮȢ țĮȚ ʌȠȣ µȠȚȐȗİȚ ʌȠȜȪ µİ IJȘȞ ȖȞȦıIJȒ ȖȜȫııĮ ʌȡȠȖȡĮµµĮIJȚıµȠȪ Visual Basic. Ǿ VBScript İȞıȦµĮIJȫȞİIJĮȚ µȑıĮ ıIJȠȞ țȫįȚțĮ IJȘȢ HTML țĮȚ Ƞ țȫįȚțȐȢ IJȘȢ µʌȠȡİȓ ȞĮ İțIJİȜİıșİȓ ĮµȑıȦȢ Ȓ ȩIJĮȞ ȜĮµȕȐȞİȚ ȤȫȡĮ ȑȞĮ ıȣµȕȐȞ (event). 1. ȉȠ ȆȡȫIJȠ ȆȡȩȖȡĮµµĮ ıİ VBScript DzȞĮ ʌȡȫIJȠ ĮʌȜȩ ʌĮȡȐįİȚȖµĮ ıİ VBScript İȓȞĮȚ IJȠ İȟȒȢ: <HTML> <BODY> <SCRIPT TYPE="text/vbscript"> document.write("Hello World") </SCRIPT> </BODY> </HTML> īȡȐijȠȣµİ IJȠȞ țȫįȚțĮ IJȘȢ VBScript ĮȞȐµİıĮ ıIJȚȢ İIJȚțȑIJİȢ (tags) IJȘȢ HTML <SCRIPT> țĮȚ </SCRIPT>, ȩʌȠȣ ıIJȘȞ ʌȡȫIJȘ İIJȚțȑIJĮ ȤȡȘıȚµȠʌȠȚȠȪµİ IJȘȞ ȚįȚȩIJȘIJĮ (attribute) TYPE="text/vbscript" Ȓ LANGUAGE="VBScript" ȖȚĮ ȞĮ ʌȡȠıįȚȠȡȓıȠȣµİ IJȘȞ ȖȜȫııĮ scripting. ȉȠ ʌĮȡĮʌȐȞȦ ıİȞȐȡȚȠ șĮ ݵijĮȞȓıİȚ ȑȞĮ țİȓµİȞȠ µȑıĮ ıIJȘȞ ȚıIJȠıİȜȓįĮ ȤȡȘıȚµȠʌȠȚȫȞIJĮȢ IJȘ ıȣȞȐȡIJȘıȘ (µȑșȠįȠ) write() IJȠȣ ĮȞIJȚțİȚµȑȞȠȣ document. 2. ȂȠȡijȠʌȠȓȘıȘ ȀİȚµȑȞȠȣ µİ VBScript ȂʌȠȡȠȪµİ ȞĮ İȚıȐȖȠȣµİ İIJȚțȑIJİȢ IJȘȢ HTML µȑıĮ ıIJȠ țİȓµİȞȠ µȚĮȢ İȞIJȠȜȒȢ document.write() ȖȚĮ ȞĮ µȠȡijȠʌȠȚȒıȠȣµİ IJȠ țİȓµİȞȠ µȚĮȢ ȚıIJȠıİȜȓįĮȢ, ȦȢ İȟȒȢ: <HTML> <BODY> <SCRIPT TYPE="text/vbscript"> document.write("<H1> Hello from Greece </H1>") document.write("<H2> Hello World </H2>") </SCRIPT> </BODY> </HTML> 3. ȈȣȞĮȡIJȒıİȚȢ ıIJȠ ȉµȒµĮ head ȅȚ ıȣȞĮȡIJȒıİȚȢ (functions) ı' ȑȞĮ script ȖȡȐijȠȞIJĮȚ ıIJȠ IJµȒµĮ head IJȘȢ ȚıIJȠıİȜȓįĮȢ țĮȚ ĮȣIJȩ ȖȚĮ ȞĮ İȓµĮıIJİ ıȓȖȠȣȡȠȚ ȩIJȚ IJȠ script șĮ ijȠȡIJȦșİȓ ʌȡȚȞ țȜȘșİȓ Ș ıȣȞȐȡIJȘıȘ. <HTML> <HEAD> <SCRIPT TYPE="text/vbscript"> alert("Hello") </SCRIPT> </HEAD> </HTML> 4. Scripts ıIJȠ ȉµȒµĮ body ȉĮ scripts ʌȠȣ ȕȡȓıțȠȞIJĮȚ ıIJȠ IJµȒµĮ body İțIJİȜȠȪȞIJĮȚ ȩIJĮȞ ijȠȡIJȫȞİIJĮȚ Ș ȚıIJȠıİȜȓįĮ. ȂʌȠȡȠȪµİ ȞĮ IJȠʌȠșİIJȒıȠȣµİ ȩıĮ scripts șȑȜȠȣµİ ı' ȑȞĮ ȑȖȖȡĮijȠ țĮȚ µʌȠȡȠȪµİ ȞĮ ȑȤȠȣµİ scripts țĮȚ ıIJȠ IJµȒµĮ head țĮȚ ıIJȠ IJµȒµĮ body. ȉĮ scripts ʌȠȣ ȣʌȐȡȤȠȣȞ ıIJȠ IJµȒµĮ body įȘµȚȠȣȡȖȠȪȞ (ĮȜȜȐȗȠȣȞ) IJȠ ʌİȡȚİȤȩµİȞȠ IJȘȢ ȚıIJȠıİȜȓįĮȢ. ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -2-

ȉĮ scripts ʌȠȣ ʌȡȑʌİȚ ȞĮ İțIJİȜİıșȠȪȞ ȩIJĮȞ țȜȘșȠȪȞ Ȓ ȩIJĮȞ ȜȐȕİȚ ȤȫȡĮ ȑȞĮ ıȣµȕȐȞ (event), ȖȡȐijȠȞIJĮȚ ıIJȠ IJµȒµĮ head µȚĮȢ ȚıIJȠıİȜȓįĮȢ. 5. ȅȚ ȂİIJĮȕȜȘIJȑȢ ıIJȘȞ VBScript īȚĮ ȞĮ įȘµȚȠȣȡȖȒıȠȣµİ µȚĮ µİIJĮȕȜȘIJȒ (variable) ıIJȘȞ VBScript, ȞĮ IJȘȢ įȫıȠȣµİ µȚĮ IJȚµȒ țĮȚ ȞĮ IJȘȞ ݵijĮȞȓıȠȣµİ ı' ȑȞĮ țİȓµİȞȠ, µʌȠȡȠȪµİ ȞĮ ȖȡȐȥȠȣµİ IJĮ İȟȒȢ: <SCRIPT TYPE="text/vbscript"> dim name name = "John" document.write("ȉȠ ȩȞȠµȐ µȠȣ İȓȞĮȚ: " & name) </SCRIPT> ȈIJȘȞ VBScript ȩȜİȢ ȠȚ µİIJĮȕȜȘIJȑȢ İȓȞĮȚ IJȪʌȠȣ variant, įȘȜĮįȒ µʌȠȡȠȪȞ ȞĮ ĮʌȠșȘțİȪıȠȣȞ įȚĮijȠȡİIJȚțȐ İȓįȘ įİįȠµȑȞȦȞ. ȉĮ ȠȞȩµĮIJĮ IJȦȞ µİIJĮȕȜȘIJȫȞ ʌȡȑʌİȚ ȞĮ ĮȡȤȓȗȠȣȞ µİ ȖȡȐµµĮ, ȞĮ µȘȞ ʌİȡȚȑȤȠȣȞ IJȠȞ ȤĮȡĮțIJȒȡĮ (.) țĮȚ ȞĮ µȘȞ ȟİʌİȡȞȐȞİ IJȠȣȢ 255 ȤĮȡĮțIJȒȡİȢ ıİ µȒțȠȢ. ȂʌȠȡȠȪµİ ȞĮ ȖȡȐȥȠȣµİ IJȘȞ İʌȚȜȠȖȒ Option Explicit ıIJȘȞ ĮȡȤȒ İȞȩȢ script, ȖȚĮ ȞĮ ĮȞĮȖțĮıIJȠȪµİ ȞĮ ȤȡȘıȚµȠʌȠȚȒıȠȣµİ µȚĮ Įʌȩ IJȚȢ İȞIJȠȜȑȢ dim, public Ȓ private ȖȚĮ ȞĮ įȘȜȫıȠȣµİ IJȚȢ µİIJĮȕȜȘIJȑȢ İȞȩȢ script, ȦȢ İȟȒȢ: option explicit dim name name = "Antonio" 6. ȅȚ ȆȓȞĮțİȢ ıIJȘȞ VBScript Ǿ įȒȜȦıȘ İȞȩȢ ʌȓȞĮțĮ (array) ıIJȘȞ VBScript ȤȡȘıȚµȠʌȠȚİȓ ʌĮȡİȞșȑıİȚȢ µİIJȐ Įʌȩ IJȠ ȩȞȠµĮ IJȘȢ µİIJĮȕȜȘIJȒȢ țĮȚ Ș ĮȡȓșµȘıȘ ȟİțȚȞȐİȚ Įʌȩ IJȠ 0. ȅȚ ʌĮȡĮțȐIJȦ İȞIJȠȜȑȢ įȘȜȫȞȠȣȞ ȑȞĮȞ ʌȓȞĮțĮ 3 șȑıİȦȞ țĮȚ IJȠȣ țĮIJĮȤȦȡȠȪȞ IJȚµȑȢ: dim names(2) name(0) = "Anna" name(1) = "Jani" name(2) = "Stella" īȚĮ ȑȞĮȞ ʌȓȞĮțĮ ʌȠȜȜȫȞ įȚĮıIJȐıİȦȞ ȤȦȡȓȗȠȣµİ IJȠȣȢ ĮȡȚșµȠȪȢ µȑıĮ ıIJȚȢ ʌĮȡİȞșȑıİȚȢ µİ țȩµµĮIJĮ. Ǿ İʌȩµİȞȘ İȞIJȠȜȒ įȘµȚȠȣȡȖİȓ ȑȞĮȞ ʌȓȞĮțĮ įȪȠ įȚĮıIJȐıİȦȞ µİ 5 ȖȡĮµµȑȢ (rows) țĮȚ 7 ıIJȒȜİȢ (columns): dim table(4, 6) ȉȠ ʌĮȡĮțȐIJȦ ıİȞȐȡȚȠ įȘµȚȠȣȡȖİȓ ȑȞĮȞ ʌȓȞĮțĮ 3 șȑıİȦȞ, țĮIJĮȤȦȡİȓ įȚȐijȠȡĮ ȠȞȩµĮIJĮ ı' ĮȣIJȩȞ țĮȚ µİIJȐ IJĮ ݵijĮȞȓȗİȚ µȑıĮ ıIJȘȞ ȚıIJȠıİȜȓįĮ. <HTML> <BODY> <SCRIPT TYPE="text/vbscript"> dim names(2) names(0) = "Anna" names(1) = "Georgia" names(2) = "Alexandra" for i=0 to 2 document.write(names(i) & "<br />") next </SCRIPT> </BODY> </HTML>

ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -3-

7. ȅȚ ǻȚĮįȚțĮıȓİȢ (Procedures) ıIJȘȞ VBScript ȂȚĮ įȚĮįȚțĮıȓĮ (procedure) IJȪʌȠȣ Sub: x

ǹʌȠIJİȜİȓIJĮȚ Įʌȩ µȚĮ ıİȚȡȐ İȞIJȠȜȫȞ ʌȠȣ ʌİȡȚțȜİȓȠȞIJĮȚ Įʌȩ IJȚȢ İȞIJȠȜȑȢ Sub țĮȚ End Sub.

x

ȂʌȠȡİȓ ȞĮ İțIJİȜȑıİȚ įȚȐijȠȡİȢ İȞȑȡȖİȚİȢ, ĮȜȜȐ įİȞ İʌȚıIJȡȑijİȚ µȚĮ IJȚµȒ.

x

ȂʌȠȡİȓ ȞĮ ʌİȡȚȑȤİȚ ȠȡȓıµĮIJĮ (arguments) ʌȠȣ IJȘȢ µİIJĮȕȚȕȐȗȠȞIJĮȚ Įʌȩ µȚĮ ȐȜȜȘ țĮȜȠȪıĮ įȚĮįȚțĮıȓĮ, İȞȫ ĮȞ įİȞ ȑȤİȚ ȠȡȓıµĮIJĮ, ʌȡȑʌİȚ ȞĮ ȑȤİȚ įȪȠ ȐįİȚİȢ ʌĮȡİȞșȑıİȚȢ ().

ǹțȠȜȠȣșİȓ ȑȞĮ ʌĮȡȐįİȚȖµĮ: <HTML> <HEAD> <SCRIPT TYPE="text/vbscript"> sub mySub() msgbox("ȂȚĮ įȚĮįȚțĮıȓĮ IJȪʌȠȣ sub") end sub </SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="text/vbscript"> call mySub() </SCRIPT> </BODY> </HTML> ǵIJĮȞ țĮȜȠȪµİ µȚĮ įȚĮįȚțĮıȓĮ IJȪʌȠȣ Sub, µʌȠȡȠȪµİ ȞĮ ȤȡȘıȚµȠʌȠȚȒıȠȣµİ IJȘȞ İȞIJȠȜȒ Call, ȦȢ İȟȒȢ: Call MyProc(argument1, argument2) Ȓ µʌȠȡȠȪµİ ȞĮ ʌĮȡĮȜİȓȥȠȣµİ IJȘȞ İȞIJȠȜȒ Call, ȦȢ İȟȒȢ: MyProc argument1, argument2 8. ȅȚ ȈȣȞĮȡIJȒıİȚȢ (Functions) ıIJȘȞ VBScript ȂȚĮ įȚĮįȚțĮıȓĮ (procedure) IJȪʌȠȣ Function: x

ǹʌȠIJİȜİȓIJĮȚ Įʌȩ µȚĮ ıİȚȡȐ İȞIJȠȜȫȞ ʌȠȣ ʌİȡȚțȜİȓȠȞIJĮȚ Įʌȩ IJȚȢ İȞIJȠȜȑȢ Function țĮȚ End Function.

x

ȂʌȠȡİȓ ȞĮ İțIJİȜȑıİȚ įȚȐijȠȡİȢ İȞȑȡȖİȚİȢ țĮȚ İʌȚıIJȡȑijİȚ µȓĮ µȩȞȠ IJȚµȒ.

x

ȂʌȠȡİȓ ȞĮ ʌİȡȚȑȤİȚ ȠȡȓıµĮIJĮ (arguments) ʌȠȣ IJȘȢ µİIJĮȕȚȕȐȗȠȞIJĮȚ Įʌȩ µȚĮ ȐȜȜȘ țĮȜȠȪıĮ įȚĮįȚțĮıȓĮ, İȞȫ ĮȞ įİȞ ȑȤİȚ ȠȡȓıµĮIJĮ, ʌȡȑʌİȚ ȞĮ ȑȤİȚ įȪȠ ȐįİȚİȢ ʌĮȡİȞșȑıİȚȢ ().

x

ǼʌȚıIJȡȑijİȚ µȓĮ IJȚµȒ ıIJȠ ȩȞȠµȐ IJȘȢ.

ǹțȠȜȠȣșİȓ ȑȞĮ ʌĮȡȐįİȚȖµĮ: <HTML> <HEAD> <SCRIPT TYPE="text/vbscript"> function myFunction() myFunction = "red" end function </SCRIPT> ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -4-

</HEAD> <BODY> <SCRIPT TYPE="text/vbscript"> document.write("ȉȠ ĮȖĮʌȘµȑȞȠ µȠȣ ȤȡȫµĮ İȓȞĮȚ: " & myFunction()) </SCRIPT> </BODY> </HTML> ǵIJĮȞ țĮȜȠȪµİ µȚĮ ıȣȞȐȡIJȘıȘ (function), µʌȠȡȠȪµİ ȞĮ ȖȡȐȥȠȣµİ IJȠ İȟȒȢ: name = findname() İįȫ țĮȜȠȪµİ µȚĮ ıȣȞȐȡIJȘıȘ µİ ȩȞȠµĮ findname țĮȚ Ș IJȚµȒ ʌȠȣ İʌȚıIJȡȑijİȚ Ș ıȣȞȐȡIJȘıȘ țĮIJĮȤȦȡİȓIJĮȚ ıIJȘ µİIJĮȕȜȘIJȒ name. DzȞĮȢ ȐȜȜȠȢ IJȡȩʌȠȢ ȖȚĮ ȞĮ țĮȜȑıȠȣµİ µȚĮ ıȣȞȐȡIJȘıȘ İȓȞĮȚ Ƞ İȟȒȢ: msgbox "ȉȠ ȩȞȠµȐ ıĮȢ İȓȞĮȚ: " & findname() İįȫ țĮȜȠȪµİ İʌȓıȘȢ µȚĮ ıȣȞȐȡIJȘıȘ µİ ȩȞȠµĮ findname țĮȚ Ș IJȚµȒ ʌȠȣ İʌȚıIJȡȑijİȚ Ș ıȣȞȐȡIJȘıȘ ݵijĮȞȓȗİIJĮȚ ı' ȑȞĮ ʌȜĮȓıȚȠ µȘȞȪµĮIJȠȢ (message box). 9. Ǿ ǼȞIJȠȜȒ If Ǿ İȞIJȠȜȒ If ȑȤİȚ IJȚȢ İȟȒȢ ʌĮȡĮȜȜĮȖȑȢ: x

If ... Then

x

If ... Then ... End If

x

If ... Then ... Else ... End If

x

If ... Then ... ElseIf ... Else ... End If

ǹțȠȜȠȣșȠȪȞ ʌĮȡĮįİȓȖµĮIJĮ: ǹȞ șȑȜȠȣµİ ȞĮ İțIJİȜȑıȠȣµİ µȓĮ µȩȞȠ İȞIJȠȜȒ ȩIJĮȞ ĮȜȘșİȪİȚ Ș ıȣȞșȒțȘ: If i = 10 Then msgbox "Hello" ǹȞ șȑȜȠȣµİ ȞĮ İțIJİȜȑıȠȣµİ ʌİȡȚııȩIJİȡİȢ Įʌȩ µȓĮ İȞIJȠȜȑȢ ȩIJĮȞ ĮȜȘșİȪİȚ Ș ıȣȞșȒțȘ: If i = 10 Then msgbox "Hello" i = 11 ... İȞIJȠȜȑȢ ... End If ǵIJĮȞ șȑȜȠȣµİ ȞĮ İțIJİȜȑıȠȣµİ țȐʌȠȚİȢ İȞIJȠȜȑȢ ĮȞ ĮȜȘșİȪİȚ Ș ıȣȞșȒțȘ țĮȚ țȐʌȠȚİȢ ȐȜȜİȢ ĮȞ įİȞ ĮȜȘșİȪİȚ Ș ıȣȞșȒțȘ: If i = 10 Then msgbox "Hello" i = 11 Else msgbox "Goodbye" End If ǵIJĮȞ șȑȜȠȣµİ ȞĮ İʌȚȜȑȟȠȣµİ µȓĮ Įʌȩ ʌȠȜȜȑȢ ȠµȐįİȢ İȞIJȠȜȫȞ ʌȡȠȢ İțIJȑȜİıȘ: i = hour(time) If i = 10 Then document.write("ȂȩȜȚȢ ȟȪʌȞȘıĮ!") ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -5-

ElseIf i = 11 Then document.write("ȆİȚȞȐȦ!") ElseIf i = 12 Then document.write("ǷȡĮ ȖȚĮ ijĮȖȘIJȩ!") ElseIf i = 16 Then document.write("ǷȡĮ ȖȚĮ İʌȚıIJȡȠijȒ!") Else document.write("DZȖȞȦıIJȠ") End If 10. Ǿ ǼȞIJȠȜȒ Select Case ȋȡȘıȚµȠʌȠȚȠȪµİ IJȘȞ İȞIJȠȜȒ Select Case ȩIJĮȞ șȑȜȠȣµİ ȞĮ İʌȚȜȑȟȠȣµİ µȓĮ Įʌȩ ʌȠȜȜȑȢ ȠµȐįİȢ İȞIJȠȜȫȞ ʌȡȠȢ İțIJȑȜİıȘ. ǹțȠȜȠȣșİȓ ȑȞĮ ʌĮȡȐįİȚȖµĮ: Select Case payment Case "Cash" msgbox "ĬĮ ʌȜȘȡȫıİIJİ µİ µİIJȡȘIJȐ" Case "Visa" msgbox "ĬĮ ʌȜȘȡȫıİIJİ µİ visa" Case Else msgbox "DZȖȞȦıIJȘ µȑșȠįȠȢ ʌȜȘȡȦµȒȢ" End Select 11. Ǿ ǼȞIJȠȜȒ For ... Next ȋȡȘıȚµȠʌȠȚȠȪµİ IJȘȞ İȞIJȠȜȒ For ... Next ȖȚĮ ȞĮ İțIJİȜȑıȠȣµİ µȚĮ ȠµȐįĮ İȞIJȠȜȫȞ ȩIJĮȞ ȖȞȦȡȓȗȠȣµİ ʌȩıİȢ İʌĮȞĮȜȒȥİȚȢ ȤȡİȚĮȗȩµĮıIJİ. Ǿ İȞIJȠȜȒ For țĮșȠȡȓȗİȚ IJȘ µİIJĮȕȜȘIJȒ µİIJȡȘIJȒ (i) țĮșȫȢ țĮȚ IJȘȞ ĮȡȤȚțȒ țĮȚ IJȘȞ IJİȜȚțȒ IJȘȢ IJȚµȒ, İȞȫ Ș İȞIJȠȜȒ Next ĮȣȟȐȞİȚ IJȠȞ µİIJȡȘIJȒ i țĮIJȐ 1. Ȃİ IJȘ ȜȑȟȘ țȜİȚįȓ Step, µʌȠȡȠȪµİ ȞĮ ĮȣȟȒıȠȣµİ Ȓ ȞĮ İȜĮIJIJȫıȠȣµİ IJȠȞ µİIJȡȘIJȒ ȩıȠ șȑȜȠȣµİ. ǹțȠȜȠȣșȠȪȞ ʌĮȡĮįİȓȖµĮIJĮ: For i = 1 to 10 ... İȞIJȠȜȑȢ ... Next For i = 2 to 10 Step 2 ... İȞIJȠȜȑȢ ... Next For i = 10 to 2 Step -2 ... İȞIJȠȜȑȢ ... Next ȂʌȠȡȠȪµİ ȞĮ İȟȑȜșȠȣµİ Įʌȩ µȚĮ İȞIJȠȜȒ For ... Next µİ IJȘ ȜȑȟȘ țȜİȚįȓ Exit For. ǹțȠȜȠȣșȠȪȞ ʌĮȡĮįİȓȖµĮIJĮ µİ IJȘȞ İȞIJȠȜȒ For ... Next: <SCRIPT TYPE="text/vbscript"> for i = 0 to 5 document.write("ȅ ĮȡȚșµȩȢ İȓȞĮȚ: " & i & "<br>") next </SCRIPT> ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -6-

ȉȠ İʌȩµİȞȠ ʌĮȡȐįİȚȖµĮ įİȓȤȞİȚ ʌȫȢ µʌȠȡȠȪµİ ȞĮ įȚȑȜșȠȣµİ µȑıĮ Įʌȩ IJȚȢ 6 İʌȚțİijĮȜȓįİȢ (headers) IJȘȢ HTML: <SCRIPT TYPE="text/vbscript"> for i = 1 to 6 document.write("<H" & i & "> next </SCRIPT>

ǼʌȚțİijĮȜȓįĮ " & i & "</H" & i & ">")

12. Ǿ ǼȞIJȠȜȒ For Each ... Next DzȞĮȢ ȕȡȩȤȠȢ For Each ... Next İʌĮȞĮȜĮµȕȐȞİȚ µȚĮ ȠµȐįĮ İȞIJȠȜȫȞ ȖȚĮ țȐșİ ıIJȠȚȤİȓȠ (item) ʌȠȣ ȣʌȐȡȤİȚ ıİ µȚĮ ıȣȜȜȠȖȒ (collection) Ȓ ȖȚĮ țȐșİ ıIJȠȚȤİȓȠ (element) İȞȩȢ ʌȓȞĮțĮ (array). Ǿ įȚĮijȠȡȐ IJȠȣ Įʌȩ IJȠȞ ȕȡȩȤȠ For ... Next İȓȞĮȚ ȩIJȚ įİȞ İȓµĮıIJİ ȣʌȠȤȡİȦµȑȞȠȚ ȞĮ țĮșȠȡȓıȠȣµİ IJȠȞ ĮȡȚșµȩ IJȦȞ ıIJȠȚȤİȓȦȞ ȖȚĮ IJĮ ȠʌȠȓĮ șȑȜȠȣµİ ȞĮ țȐȞȠȣµİ IJȠȞ ȕȡȩȤȠ. ǹțȠȜȠȣșİȓ ȑȞĮ ʌĮȡȐįİȚȖµĮ: dim names(2) names(0) = "George" names(1) = "John" names(2) = "Antonio" For Each x in names document.write(x & "<br />") Next 13. Ǿ ǼȞIJȠȜȒ Do ... Loop Ȃİ IJȘȞ İȞIJȠȜȒ (ȕȡȩȤȠ) Do ... Loop µʌȠȡȠȪµİ ȞĮ İțIJİȜȑıȠȣµİ µȚĮ ȠµȐįĮ İȞIJȠȜȫȞ ȩIJĮȞ įİȞ ȖȞȦȡȓȗȠȣµİ ʌȩıİȢ İʌĮȞĮȜȒȥİȚȢ ʌȡȑʌİȚ ȞĮ ȖȓȞȠȣȞ. ȅȚ İȞIJȠȜȑȢ İʌĮȞĮȜĮµȕȐȞȠȞIJĮȚ ȖȚĮ ȩıȠ įȚȐıIJȘµĮ İȓȞĮȚ ĮȜȘșȒȢ µȚĮ ıȣȞșȒțȘ (while) Ȓ µȑȤȡȚȢ ȩIJȠȣ ȞĮ ȖȓȞİȚ ĮȜȘșȒȢ µȚĮ ıȣȞșȒțȘ (until). ȂʌȠȡȠȪµİ ȞĮ İȟȑȜșȠȣµİ Įʌȩ µȚĮ İȞIJȠȜȒ Do ... Loop ȤȡȘıȚµȠʌȠȚȫȞIJĮȢ IJȘ ȜȑȟȘ țȜİȚįȓ Exit Do. ǹțȠȜȠȣșȠȪȞ ʌĮȡĮįİȓȖµĮIJĮ: Do While i > 10 ... İȞIJȠȜȑȢ ... Loop Do ... İȞIJȠȜȑȢ ... Loop While i > 10 Do Until i = 10 ... İȞIJȠȜȑȢ ... Loop Do

... İȞIJȠȜȑȢ ... Loop Until i = 10 Do Until i = 10 i = i - 1 If i < 10 Then Exit Do ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -7-

Loop i = 0 Do While i < 10 document.write(i & "<br />") i = i + 1 Loop 14. ȅȚ ȈȣȞĮȡIJȒıİȚȢ UCase() țĮȚ LCase() ȅȚ ıȣȞĮȡIJȒıİȚȢ UCase() țĮȚ LCase() µİIJĮIJȡȑʌȠȣȞ ȑȞĮ string ıİ țİijĮȜĮȓĮ Ȓ ʌİȗȐ ȖȡȐµµĮIJĮ ĮȞIJȓıIJȠȚȤĮ. <SCRIPT TYPE="text/vbscript"> txt = "Hello World!" document.write(UCase(txt)) document.write("<br />") document.write(LCase(txt)) </SCRIPT> 15. ȅȚ ȈȣȞĮȡIJȒıİȚȢ Trim(), LTrim() țĮȚ RTrim() Ǿ ıȣȞȐȡIJȘıȘ Trim() İʌȚıIJȡȑijİȚ ȑȞĮ string ȤȦȡȓȢ IJĮ ĮȡȤȚțȐ țĮȚ IJĮ IJİȜȚțȐ țİȞȐ, Ș ıȣȞȐȡIJȘıȘ LTrim() İʌȚıIJȡȑijİȚ ȑȞĮ string ȤȦȡȓȢ IJĮ ĮȡȤȚțȐ țİȞȐ țĮȚ Ș ıȣȞȐȡIJȘıȘ RTrim() İʌȚıIJȡȑijİȚ ȑȞĮ string ȤȦȡȓȢ IJĮ IJİȜȚțȐ țİȞȐ. <SCRIPT TYPE="text/vbscript"> txt = " World " document.write("Hello" & Trim(txt) & "Greece <br />") document.write("Hello" & LTrim(txt) & "Greece <br />") document.write("Hello" & RTrim(txt) & "Greece <br />") </SCRIPT> 16. Ǿ ȈȣȞȐȡIJȘıȘ StrReverse() Ǿ ıȣȞȐȡIJȘıȘ StrReverse() ĮȞĮıIJȡȑijİȚ ȑȞĮ string. <SCRIPT TYPE="text/vbscript"> txt = "Hello from Greece!" document.write(strReverse(txt)) </SCRIPT> 17. Ǿ ȈȣȞȐȡIJȘıȘ Round() Ǿ ıȣȞȐȡIJȘıȘ Round() ıIJȡȠȖȖȣȜȠʌȠȚİȓ ȑȞĮȞ ĮȡȚșµȩ ıIJȠȞ ʌȜȘıȚȑıIJİȡȠ ĮțȑȡĮȚȠ. <SCRIPT TYPE="text/vbscript"> i = 5.67 j = 5.34 document.write(Round(i)) document.write("<br />") document.write(Round(j)) </SCRIPT> 18. ȅȚ ȈȣȞĮȡIJȒıİȚȢ Rnd() țĮȚ Randomize() ȅȚ ıȣȞĮȡIJȒıİȚȢ Rnd() țĮȚ Randomize() ȤȡȘıȚµȠʌȠȚȠȪȞIJĮȚ ȖȚĮ IJȘȞ ʌĮȡĮȖȦȖȒ IJȣȤĮȓȦȞ ĮȡȚșµȫȞ. <SCRIPT TYPE="text/vbscript"> Randomize() ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -8-

document.write(Rnd()) </SCRIPT> 19. Ǿ ȈȣȞȐȡIJȘıȘ Int() Ǿ ıȣȞȐȡIJȘıȘ Int() ĮʌȠțȩʌIJİȚ IJȠ įİțĮįȚțȩ µȑȡȠȢ İȞȩȢ ĮȡȚșµȠȪ. <SCRIPT TYPE="text/vbscript"> Randomize() i = Int(100 * Rnd()) document.write("DzȞĮȢ IJȣȤĮȓȠȢ ĮȡȚșµȩȢ: <b>" & i & "</b>") </SCRIPT> 20. ȅȚ ȈȣȞĮȡIJȒıİȚȢ Left() țĮȚ Right() ȅȚ ıȣȞĮȡIJȒıİȚȢ Left() țĮȚ Right() İʌȚıIJȡȑijȠȣȞ ȑȞĮȞ țĮșȠȡȚıµȑȞȠ ĮȡȚșµȩ ȤĮȡĮțIJȒȡȦȞ Įʌȩ IJȘȞ ĮȡȚıIJİȡȒ Ȓ IJȘ įİȟȚȐ ʌȜİȣȡȐ İȞȩȢ string. <SCRIPT TYPE="text/vbscript"> text = "Hello from Greece!" document.write(Left(txt, 5)) document.write("<br />") document.write(Right(txt, 7)) </SCRIPT> 21. Ǿ ȈȣȞȐȡIJȘıȘ Replace() Ǿ ıȣȞȐȡIJȘıȘ Replace() ĮȞIJȚțĮșȚıIJȐ µİȡȚțȠȪȢ ȤĮȡĮțIJȒȡİȢ ı' ȑȞĮ string. <SCRIPT TYPE="text/vbscript"> text = "Hello World!" document.write(Replace(txt, "World", "Greece")) </SCRIPT> 22. Ǿ ȈȣȞȐȡIJȘıȘ Mid() Ǿ ıȣȞȐȡIJȘıȘ Mid() İʌȚıIJȡȑijİȚ ȑȞĮȞ țĮșȠȡȚıµȑȞȠ ĮȡȚșµȩ ȤĮȡĮțIJȒȡȦȞ Įʌȩ ȑȞĮ string. <SCRIPT TYPE="text/vbscript"> text = "Hallo from Greece!" document.write(Mid(txt, 12, 6)) </SCRIPT> 23. ȈȣȞĮȡIJȒıİȚȢ ǾµİȡȠµȘȞȓĮȢ țĮȚ ǷȡĮȢ ȂʌȠȡȠȪµİ ȞĮ ݵijĮȞȓıȠȣµİ IJȘȞ ȘµİȡȠµȘȞȓĮ țĮȚ IJȘȞ ȫȡĮ µİ IJȚȢ ıȣȞĮȡIJȒıİȚȢ Date() țĮȚ Time(). <SCRIPT TYPE="text/vbscript"> document.write("Ǿ ȘµİȡȠµȘȞȓĮ İȓȞĮȚ: " & date()) document.write("<br />") document.write("Ǿ ȫȡĮ İȓȞĮȚ: " & time()) </SCRIPT> Ȃİ IJȘȞ ıȣȞȐȡIJȘıȘ Hour(), µʌȠȡȠȪµİ ĮȞȐȜȠȖĮ µİ IJȘȞ ȫȡĮ IJȘȢ ȘµȑȡĮȢ, ȞĮ ݵijĮȞȓȗȠȣµİ țĮIJȐȜȜȘȜȠ µȒȞȣµĮ. <SCRIPT TYPE="text/vbscript"> i=hour(time) if i<11 then document.write("ȀĮȜȘµȑȡĮ") elseif i>=11 and i<16 then document.write("ȀĮȜȩ ȂİıȘµȑȡȚ") ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


ȆĮȡĮįİȓȖµĮIJĮ VBScript -9-

elseif i>=16 and i<20 then document.write("ȀĮȜȩ ǹʌȩȖİȣµĮ") else document.write("ȀĮȜȩ ǺȡȐįȣ") end if </SCRIPT> ȂʌȠȡȠȪµİ ȞĮ ݵijĮȞȓıȠȣµİ IJȠȞ IJȡȑȤȠȞIJĮ µȒȞĮ țĮȚ IJȘȞ IJȡȑȤȠȣıĮ ȘµȑȡĮ µİ IJȚȢ ıȣȞĮȡIJȒıİȚȢ MonthName() țĮȚ WeekDayName(). <SCRIPT TYPE="text/vbscript"> document.write("Ǿ ȘµȑȡĮ İȓȞĮȚ: " & WeekdayName(weekday(date))) document.write("<br />") document.write("ȅ µȒȞĮȢ İȓȞĮȚ: " & MonthName(month(date))) </SCRIPT> Ȃİ ȤȡȒıȘ IJȘȢ İȞIJȠȜȒȢ select case țĮȚ IJȘȢ ıȣȞȐȡIJȘıȘȢ weekday(date) µʌȠȡȠȪµİ ȞĮ İȜȑȖȟȠȣµİ IJȘȞ IJȡȑȤȠȣıĮ ȘµȑȡĮ țĮȚ ȞĮ ݵijĮȞȓıȠȣµİ ȑȞĮ ĮȞȐȜȠȖȠ µȒȞȣµĮ ȖȚĮ IJȠ ʌȠȚĮ ȘµȑȡĮ İȓȞĮȚ: <SCRIPT TYPE="text/vbscript"> d=weekday(date) select case d case 1 document.write("ȀȣȡȚĮțȒ ȖȚĮ ȟİțȠȪȡĮıȘ") case 2 document.write("ǹȞȚĮȡȒ ǻİȣIJȑȡĮ!") case 3 document.write("ǹțȩµȘ ȉȡȓIJȘ İȓȞĮȚ!") case 4 document.write("ȉİIJȐȡIJȘ!") case 5 document.write("ȆȑµʌIJȘ...") case 6 document.write("ǼʌȚIJȑȜȠȣȢ ȆĮȡĮıțİȣȒ!!!") case else document.write("ȈȐȕȕĮIJȠ ȖȚĮ ȘȡݵȓĮ!") end select </SCRIPT> ȂʌȠȡȠȪµİ ȞĮ ȤȡȘıȚµȠʌȠȚȒıȠȣµİ IJȘ ıȣȞȐȡIJȘıȘ IsDate() ȖȚĮ ȞĮ İȜȑȖȟȠȣµİ ĮȞ µȚĮ ȑțijȡĮıȘ ĮʌȠIJİȜİȓ ȘµİȡȠµȘȞȓĮ. Ǿ ıȣȞȐȡIJȘıȘ İʌȚıIJȡȑijİȚ IJȘȞ IJȚµȒ true ĮȞ Ș ȑțijȡĮıȘ ĮʌȠIJİȜİȓ ȘµİȡȠµȘȞȓĮ țĮȚ false ĮȞ ȩȤȚ. <SCRIPT TYPE="text/vbscript"> somedate = "16/04/2004" document.write(IsDate(somedate)) </SCRIPT>

ǹș. Ǽ. ȀȠȣIJıȠȞȚțȩȜĮȢ


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.