Вы находитесь на странице: 1из 44

VBScript

, VBScript
. ,

. HTML,
, 2- , 2008..
VBScript , JavaScript.

. VBScript ,
JavaScript, , , .
, JavaScript,
VBScrip, JavaScript:
VBScript ;

');


=, (Dim, Let,
Set, Private, Public);
;
;
, ,
;
,
.

13.1.
MsgBox, InputBox.
alert()
prompt() JavaScript.

13.1.1. MsgBox
;
, , . :
MsgBox(, ,
) MsgBox ,

MsgBox ,
:
x=MsgBox(" !", 20, "")

. ,
. ,
, . 13.1.
13.1. ,

vbOkOnly

OK

vbOkCancel

OK, (Cancel)

vbAbortRetryIgnore

(Abort),
(Ignore)

vbYesNoCancel

(Yes), (No), (Cancel)

vbYesNo

(Yes), (No)

vbRetryCancel

(Retry), (Cancel)

vbDefaultButtonI

vbDefaultButton2

256

vbDefaultButton3

512

vbDefaultButton4

768

(Retry),

, , . 13.2.
13.2. ,

vbCritical

16

vbQuestlon

32

vbExclamation

48

vblnformatlon

64

"i"

, . 13.3,
.

13.3. MsgBox

vbApplicationModal

vbSystemModal

4096

,
. ,
(Yes) (No),
, 4 + 32 = 36.
(. 13.1):
MsgBox " !", 20, ""

. 13.1. , MsgBox

. 13.4
intButtonClicked.

13.4. MsgBox

VbOK

OK

vbCancel

(Cancel)

vbAbort

(Abort)

vbRetry

(Retry)

vbIgnore

(Ignore)

vbYes

(Yes)

vbNo

(No)

, ,
Chr(13),
. ,
MsgBox "!" + Chr(13) + " OK"

13.1.2. InputBox
OK
(Cancel). , OK,
Empty, (Cancel).
:
InputBox(, , _[, x,
y]) InputBox , , _[,
x, y]

InputBox ,
:
x=InputBox(" ", " ", "", 1000, 2000)

,
, _ , x y

,
(1 = 1/1440 ). . 13.2 .

. 13.2. , InputBox

, , MsgBox InputBox
alert(), confirm() prompt() window.

13.2.
VBScript, JavaScript,
. , ,
.
, .
.

, , ( ).


, . 13.5.
13.5.
VBScript

String (
)

" !"
".. 123-4567"
"
30.11.2004."

Integer ()

32 768
32 767

Long ()

2 147 483 648 2 147 483 647

Single ()



3.4e38 1.4e45
1.4e45 3.4e38

Double ()

1.8e308

4.9e324

4.9e324
1.8e308

Byte ()

0 255

Currency
()
Boolean
(,
)

922 337 203 685 477.5808


922 337 203 685 477.5808
True
False

True (, ) False (,
);
,
1.01.100 31.12.9999

Date ()
Null

Null


Null,

Empty ()

Empty


Empty, ,

Error

Object ()

.
, 6 .

. 3.14 , Byte
, Integer. x
Integer Cint(x), Byte
CByte(x).
. 13.7.1.
:
15
'
-25.67
'
-28.5e-3
'
" !"
'

,
#.
(-), (/),
(:); .
( ) .
:
#8-12-2004#
#8-12-2004 17:12:36#
#8/12/2004#
#8/12/2004 17:12#

Empty , .
0
. Empty
,
.
Null .
,
. Empty,
Null .
!
VBScript, JavaScript,
. .

13.3.
. ,
, .
,

, .
, .
Dim:
Dim _

Dim , :
Dim x, y, myvar

, Dim ,
, Empty. ,
HTML- 13.1.

<HTML>
<SCRIPT LANGUAGE=VBScript>
Dim x
MsgBox x=Empty, 0, " x Empty?"
</SCRIPT>
</HTML>

HTML- Internet Explorer


, . 13.3.

. 13.3. ,

, :

1
3
.
1
.

Let .

Set ,

CreateObject().

_=
Let _=
Set _=

:
x=" !"
Let
NumberOfDays=365
Set MyTextBox=txtcontrol
MyTextBox.Value=" !"

Dim Private Public

() .

Dim ,
. (
) (

13.4.

). ( 60 ).
:
Dim _(n)

D
i
m

n 1.
0. , n

,
1
. , 8
3 :

Dim myarray(7, 2)

_()

, myarray,
3- 2- , :
myarray(2, 1)


. , ,
. ,
:
Dim _()

,
.


( ):

Redim _(n)

n 1.
:
Dim myarray() '
Redim myarray(1)
' ,
2 '
myarray(0)=""
myarray(1)=""
' : "
" MsgBox myarray(0) + " " + myarray(1)

,
.
:
Dim myarray()
'
Redim myarray(1)
' ,
2 '
myarray(0)=""
myarray(1)=""
Redim
myarray(2)
' , 3
myarray(2)=3.14
' 3-
' :
MsgBox myarray(0) + " " + myarray(1)


Preserve ():
Redim Preserve _(n)

:
Dim myarray()
'
Redim myarray(1)
' ,
2 '
myarray(0)=""
myarray(1)=""
Redim Preserve myarray(2)
' ,
3 myarray(2)=3.14
' 3-
' : "
3.14"
MsgBox myarray(0) + " " + myarray(1) + " " + str(myarray(2))

, , .
:

Lbound ;

Ubound .
1 .
myarray 1
:
Redim Preserve myarray(Ubound(myarray) + 1)

13.5.
, , .
, .
:
Const _=

, .
VBScript ,
. 13.613.13.
13.6.

vbBlack

&h00

vbRed

&hFF

vbGreen

&hFF00

vbYellow

&hFFFF

vbBTue

&hFF0000

vbMagenta

&hFF00FF

vbCyan

&hFFFF00

vbWhite

&hFFFFFF

13.7.

vbBinaryCompare

vbTextCompare

vbOatabaseCompare

13.8.

vbSunday

vbMonday

vbTuesday

vbWednesday

vbThursday

vbFrlday

ybSaturday

vbFirstJan1

, 1

vbFirstFourDays

,
, 4

vbFlrstFullWeek

vbUseSystem

vbUseSystemDayOfWeek

13.9.

vbGeneralDate

/ ,
.

. . ,
1,

vbLongDate

vbShortDate

vbLongTime

vbShortTime

13.10.

ForReading

ForWritIng

ForAppending

13.11.

vbCr

Chr(13)

vbCrLf

Chr(13) & Chr(10)

vbLf

Chr(10)

vbNewLine

, 0

vbNullChar

Chr(0)

vbNullString

,
(
)

vbTab

Chr(9)

13.12.

TristateTrue

TristateFalse

TristateUseDefault

13.13.

vbEmpty

( )

vbNull

vblnteger

vbLong

vbSingle

vbDouble

vbCurrency

vbDate

vbStnng

vbObject

vbError

10

vbBoolean

11

vbVariant

12

(
)

vbDataObject

13

vbDecimal

14

vbByte

17

vbArray

8912

13.6.
VBScript
.

. ( )
.
.

JavaScript ,
.
.
.

13.6.1.
,
. (
). , , ,
.
, . .
.

:
' ,
'
x=""
'

MsgBox x
'

,
Rem.

JavaScript //,
/*...*/.

13.6.2.
(. 13.14)
.
. , +
, . . .
13.14.

X+Y

X-Y

X*Y

X/Y

X^Y

13.6.3. 3.
,
(. 13.15) ( ),
, . . True False. , (, ),
True. False.
13.15.

X=Y

<>

X<>Y

>

X>Y

>=

( )

X>=Y

<

X<Y

<=

( )

X<=Y

,
. VBScript
.

JavaScript =,
=.

13.6.4.
, ,
, .
() (. 13.16).
13.16.

Not

()

!X

And

X And Y

Or

X Or Y

Xor

X Xor Y

Eqv

X Eqv Y

Imp

X Imp Y

True () False ().


, !, && ||
JavaScript (. . 11.7.5). ,
, , , . 13.17.
13.17. ,

X Imp Y

Not X Or Y

X Eqv Y

(X Imp Y) And (Y Imp X)

X Xor Y

(X And Not Y) Or (Not X And Y)


JavaScript , .
,
.

13.6.5.
().
.
+ &.
:
x=""
MsgBox x + " " + ""
' " "
MsgBox x & " " & "" ' "
"

JavaScript , , , ,
.
("" + ). VBScript
" ".
"1" + 2 . ,
"" + "1" + 2 .
, VBScript
,
0. "" + "1" + 2
, , , 1, ,
.

13.6.6. 6.
If
If

..., ..., ...

If :
If Then
If Then

End If
If _1 Then
[_1]
[ElseIf _2 Then

[_2]] ...

[Else
[_n]]
End If

,
. ,
ElseIf , Else
.

Select Case

, Select Case:
Select Case _
[Case __1
[_1]]
[Case __2
[_2]]
...
[Case __n
[_n]]
[Case Else
[_n+1]
] End Select

,
.
.
.
Select Case
,
Case. -
- Case, ,
. ,
Case.
Case
,
End Select. ,
Case, ,
Case Else ( ). Case
Else
, ,
End Select.
:
x=InputBox(" ?")
Select Case x
Case Empty

MsgBox " "


Case 20,21,22,23,24,25
MsgBox " "
Case 40,41,42,43,44,45
MsgBox " "
Case Else
MsgBox " "
End Select

JavaScript Select Case switch.

13.6.7. 7.

, . VBScript
.

For ... Next


:
For =_ To _ [Step ]

Next

.
.
, ,
. ,
, Next.
,
Step ( , 1).
.
:
'
Dim myarray(9)
For i=1 To 10
myarray(i-1)=x^2
Next


, Exit For.

JavaScript break.

For Each ... Next


For Each...Next
, .

. :
For Each In

Next

.
,

.
:
'
Dim myarray(25)
Count=0
For Each i In myarray
Count=Count + 1
Next

13.2 HTML-, ,
.
( )
VALUE. . 13.4.

<HTML>
<HEAD><TITLE> </TITLE></HEAD>
<FORM NAME=myform>
<INPUT TYPE=TEXT NAME=field VALUE ="">
<P>
<INPUT TYPE="BUTTON" NAME=mybutton VALUE=" "
</FORM>
<P>
<SCRIPT LANGUAGE=VBS>
For Each i In document.forms
document.write(" " + i.name + "<br>")
For Each j In i.elements
document.write(" " + j.name + " - " + j.value + "<br>")
Next
Next
</SCRIPT></HTML>

1
3
.
2
.

. 13.4.

13.3 , ,
( all
document). HTML- . 13.5.

<HTML>
<HEAD><TITLE> </TITLE></HEAD>
<FORM NAME=myform>
<INPUT TYPE=TEXT NAME=field VALUE ="">
<P>
<INPUT TYPE="BUTTON" NAME=mybutton VALUE=" "
</FORM>
<P>
<SCRIPT LANGUAGE=VBS>
For Each i In document.all
document.write(i.tagname+"<br>")
Next
</SCRIPT>
</HTML>

1
3
.
3
.

. 13.5.


,
Exit For.

Do ... Loop
Do...Loop ,
, , ( ).
:
Do While

Loop
,
. ,

( ).
,
.
:
Do While strDayOfWeek <> "" And strDayOfWeek <> ""
MsgBox (", !")
...

Loop

Do

Loop While
,

, .
.
( ).
Do Until

Loop

Do While...Loop ,
, ( until ,
).
Do

Loop Until

Do...Loop While... ,
, .

While
While :
While

Wend

, .

13.7.
( ),
, .
, .
, .
,
.
.
,
. VBScript ,
:
, , ,

myfunc prm1, prm2, prm3

, ,
myfunc(prm1, prm2, prm3)

. ,
:
document.write "<h1></h1>"
document.write("<h1></h1>")

,
, :
x = 2 + sqr(9)

' x 5

13.7.1. 1.
VBScript ,
:
;
;
/;
;
;
;
;
.

.
(. 13.18).

.

13.18.

As

ANSI

AscB

To , .

AscW

To , Unicode.

Wide, Unicode ANSI


Chr

ChrB

To , ,
.

ChrW

To , Unicode.
Wide, Unicode ANSI

Cbool

, (Boolean)

Cbyte

, "" (Byte)

Cdate

, (Date)

Dbl

,
(Double)

Cint

,
(Integer)

CLng

,
(Long)

CSng

,
(Single)

CStr

, (String)

Fix

Hex

Int

Oct

Round

Sgn

/ (. 13.19).

.

13.19. /

Date

DateAdd

DateDiff

DatePart

DateSeria1

Date ,

DateValue

Date

Day

1 31,

Hour

0 23,

Minute

0 59,

Month

1 12,

MonthName

Now

Second

0 59,

Time

Date

TimeSerial

Date ,

imeValue

Date,

Weekday

WeekdayName

Year

(. 13.20).
,
.

13.20.

Atn

Cos

e (~2.71828) ()

Log

Randomize

Rnd

Sin

Sqr

Tan

JavaScript Math
Math.().

(. 13.21).
, .
13.21.

CreateObject

ActiveX OLE
Automation

GetObject

ActiveX OLE Automation

LoadPicture

(. 13.22).
.
13.22.

Script Engine

, ,

ScriptEngineMajorVersion

Scri ptEngineMinorVersion

ScriptEngineBundVersion

(. 13.23).
.
13.23.

Filter

FormatCurrency

FormatDateTime

FormatNumber

FormatPercent

InStr

InStrB

To , .
,

InStrRev

To , InStr,

Join

Lcase

Left

LeftB

To , .

Len

LenB

To , .

Ltrim

Mid

MidB

To , .

Repl

Right

RightB

To , .

Rtrim

Space

Split

StrComp

String

StrReverse

"" ,

rim

Ucase

(. 13.24).
,
.
13.24.

IsArray

(True/False), ,

IsDate

(True/False), ,

/

IsEmpty

(True/False), ,

IsNull

(True/False), ,

IsNumeric

(True/False), ,

IsObject

(True/False), ,

ActiveX OLE Automation

VarType

.
On Error Resume Next ,

.
Err error, .
VBScript , ,
, Err.

13.5.2.

.
:
Function
_([])

[_=]
End Function

.
, _=.
, .
:
Function Srectangle(a, b)
Srectangle=a*b
End Function

:
Sub _([])

End Sub

,
.
:
Sub ToDay(xdate)
Dim xstr
xstr=" "
If xdate =Empty Then
xstr=xstr + Cstr(Now()
Else
xstr=xstr + Cstr(xdate)
End If

MsgBox(xstr)

End Sub

. ,
, .
.
:
Srectangle(5, 8)
Srectangle 5, 7+3
ToDay(#10-11-2004#)
ToDay #10-11-2004#
ToDay("")

'
'
'
'
'

40
50
" 10.11.2004"

Call.
.
:
Call Srectangle(5, 8)
Call ToDay("")

, ,
( )
- .
:
Dim TotalS, a, b, x, y
a=2
b=5
x=10
y=8.5
TotalS=Srectangle(a, b) + Srectangle(x, y) '
'

:
. ,
, ,
.
,
.
.
-. ,
,
, , .
x=1
myfunc .
.
myfunc MsgBox,
x. myfunc.

, x
, .
:
x=1
Function myfunc(x)
x=2
End Function
myfunc x
MsgBox(x)
' x, 2

:
x=1
Function myfunc(x)
x=2
End Function
myfunc(x)
MsgBox(x)
' x, 1


,
ByVal.
:
x=1
Function myfunc(ByVal x)
x=2
End Function
myfunc(x)
MsgBox(x)
' x,
1 myfunc x
MsgBox(x)
' x, 1

JavaScript
var
(
).

13.8.

VBScript , JavaScript:
.()

,
. , alert("!")
VBScript :
window.alert("!")
window.alert "!"

,
, :

_()

13.4 onfocus() onclick


.

<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BUTTON ID=mybt> </BUTTON>
<SCRIPT LANGUAGE=VBScript>
Sub window_onfocus()
window.alert(" ")
End Sub
Function mybt_onclick()
MsgBox " id=" + document.all.mybt.id
End Function
</SCRIPT>
</HTML>

. 13.6. HTML- .

. 13.6. mybt_onclick

1
3
.
4
.

Оценить