• No results found

vbscript

N/A
N/A
Protected

Academic year: 2021

Share "vbscript"

Copied!
73
0
0

Loading.... (view fulltext now)

Full text

(1)

VBScript Language

VBScript Language

for

for

IV Year IT Students

IV Year IT Students

(2)

2 2

E-Commerce

E-Commerce

Electronic Commerce

Electronic Commerce -- the

the use o

use of Web

f Web sit

sites t

es to

o

sell goods and services over the Internet.

sell goods and services over the Internet.

The World Wide Web (Web)

The World Wide Web (Web) -- a b

a bod

ody of

y of sof

softw

tware

are

and a set of protocols and

and a set of protocols and conventions based on

conventions based on

hypertext and multimedia that make the Internet

hypertext and multimedia that make the Internet

easy to use and browse.

easy to use and browse.

hypertext

hypertext -- a ca

a capabi

pability to

lity to jump

jump betw

between an

een and

d

within documents virtually at will.

within documents virtually at will.

client/serve

client/server

r computing

computing -- pro

proces

cessin

sing is s

g is sha

hared

red

between multiple small computers known as

between multiple small computers known as

clients

clients that are connected via a network to a

that are connected via a network to a host

host

computer known as a server.

computer known as a server.

uniform resource locator (URL)

uniform resource locator (URL) ±

 ± pr

prot

otoc

ocol t

ol tha

hatt

allows your browser to communicate with remote

allows your browser to communicate with remote

hosts.

(3)

2 2

E-Commerce

E-Commerce

Electronic Commerce

Electronic Commerce -- the

the use o

use of Web

f Web sit

sites t

es to

o

sell goods and services over the Internet.

sell goods and services over the Internet.

The World Wide Web (Web)

The World Wide Web (Web) -- a b

a bod

ody of

y of sof

softw

tware

are

and a set of protocols and

and a set of protocols and conventions based on

conventions based on

hypertext and multimedia that make the Internet

hypertext and multimedia that make the Internet

easy to use and browse.

easy to use and browse.

hypertext

hypertext -- a ca

a capabi

pability to

lity to jump

jump betw

between an

een and

d

within documents virtually at will.

within documents virtually at will.

client/serve

client/server

r computing

computing -- pro

proces

cessin

sing is s

g is sha

hared

red

between multiple small computers known as

between multiple small computers known as

clients

clients that are connected via a network to a

that are connected via a network to a host

host

computer known as a server.

computer known as a server.

uniform resource locator (URL)

uniform resource locator (URL) ±

 ± pr

prot

otoc

ocol t

ol tha

hatt

allows your browser to communicate with remote

allows your browser to communicate with remote

hosts.

(4)

3 3

Displaying Information with a

Displaying Information with a

Browser

Browser

When

When dispdisplaylaying ing infoinformatrmation ion sensent to t to it frit from om the the serserverver, th, thee browser processes formatting instructions included in the browser processes formatting instructions included in the text file retrieved from the

text file retrieved from the server.server. Th

The e seservrver er ststorores es ththe e dadata ta wiwithth tagstags to indicate how text andto indicate how text and other forms of information will be displayed.

other forms of information will be displayed. The

The tatags gs in tin the he WorlWorld d Wide Wide Web Web arare pae part rt of a of a spspececialial publishing language called

publishing language called hypertext markup languagehypertext markup language (HTML)

(HTML).. Do

Docucumementnts s on ton the he Web Web arare ree refeferrrred ed to ato ass Web pagesWeb pages andand their location is a

their location is a Web siteWeb site.. Sin

Since Hce HTML iTML is stas standndarard for ad for all coll compumputetersrs, an, any Web by Web brorowswser er  can request an HTML docum

(5)

4

Commonly Used HTML tags

Tag Operation Example Result <B> Boldface text <B>Hello</B> Hello

<I> Italicized text <I>Hello</I> Hello

<CENTER> Centered text <CENTER>Hello</CEN TER>

Hello

<BR> Line Break End this line.<BR> Start another line.

End this line. Start another line. <HTML> Begin/End HTML <HTML>«</HTML> Begins and Ends

Web Page <TITLE> Begin/End Title of 

Web Page

<TITLE>Web page for  CH. 13</TITLE>

³Web page for CH. 13´ appears in header bar of   browser.

(6)

5

Commonly Used HTML tags (Cont.)

Tag Operation Example Result <BODY> Begin/End Body

of Web page

<BODY>«</BODY> Begins and ends the  body of the web page <P> Start/End  paragraph <P>Paragraph</P><P>New Paragraph</P> Paragraph  New Paragraph <H1> Create type 1 (largest) heading (also 2, 3, 4) <H1>Biggest</H1> <H2>Big</H2> <H4>Smallest</H4>

Biggest

Big Smallest <IMG SRC = «> Include image in web page

<image src = ³family.jpg´> jpg image file named family is displayed <FORM> Create an input

form on a web  page

<FORM NAME = Order>«</FORM>

Creates an input form named ³Order´

<INPUT TYPE = text>

Create text box for  input

<INPUT TYPE = text  NAME = txtOrder>

A text box for input is displayed

(7)

6

N

otes About HTML

HTML tags are enclosed in angle brackets

(< >) to set them off.

Most HTML tags work in pairs, but there

are some exceptions.

Script placed within the <HEAD>..</HEAD>

tags is not executed unless referred to in

the body of the HTML page.

Script placed in the body is automatically

executed as the Web page is loaded into

the browser.

(8)

7

G

eneral Form of Web Page

<HTML> <HEAD> <TITLE>

Title of Web page goes here </TITLE>

Other heading material goes here </HEAD>

<BODY>

Body of Web page goes here </BODY>

(9)

8

S

cripting in the Browser 

An important use of Web browsers in electronic commerce is to enter information.

Validation of input and calculations on the browser can be carried out via a type of computer programming known as scripting.

Scripting is similar to other types of computer programming in that it uses variables and statements to carry out a desired set of logical operations, but it is also different.

Instead of being executed as a compiled program, it is

executed by another program, in our case the Web browser.

Scripting languages are easier to work with than compiled

languages.

A script takes longer to run than a compiled program since each instruction is being interpreted rather than being

(10)

9

S

cripting Languages

The two most widely used scripting languages for working with Web pages are Javascript and VBScript.

Javascript uses a C-like syntax and can run on either 

Netscape or Internet Explorer browsers.

VBScript is based on Visual Basic, but runs only on Internet

Explorer.

Both Javascript and VBScript, when used on the browser, are termed client-side scripting since they are running on the Web client.

VBScript is also widely used in the Active Server Page

(ASP) approach to directing Microsoft Web server software

that runs on Windows NT or 2000 operating systems. This is called server-side scripting.

(11)

Wh

at is VBScript?

An interpreted programming language

 primarily used to write relatively short programs

that access operating system and application

objects. Such languages are referred to as

"scripting" languages. The VBScript commands

are a subset of the more complete, compiled

(12)

Why

is VBScript used?

VBScript is an eas

y

to learn language and t

h

us is

a good candidate for a brief introduction to

programming.

VBScript, VB, and VBA (Visual Basic for

Applications) are collectivel

y

used more t

h

an an

y

ot

h

er language in t

h

e world so exposure to t

h

ese

languages is important.

VBScript can be used to automate tasks in web

pages, MS Excel spreads

h

eets, MS Access

Databases, and ot

h

er MS applications.

VBScript can be used to automate administrative

tasks suc

h

as moving or cop

y

ing files, creating

(13)

C

reating/Editing a VBScript file

VBScript programs are AS

C

II text files and can be edited

using an

y

text editor suc

h

as notepad.exe or pfe.exe.

Open

y

our ITE

C

110 student folder and create a folder

named lab

2

wit

h

in it.

As was discusses in lab 1 please verif

y

t

h

at t

h

e

tools->folder options->view tab->

h

ide file extensions for

known file t

y

pes is NOT c

h

ecked.

Open t

h

e lab

2

folder. Rig

h

t click an

y

w

h

ere in t

h

e

folder and c

h

oose "new text file" from t

h

e resulting

drop down menu. Rename t

h

e new text file to

Hello

W

orld.vbs .

Rig

h

t click on t

h

e new file, c

h

oose EDIT, and enter t

h

e

VBScript program on t

h

e next slide.

(14)

Wh

at is VBScript

Based on t

h

e Visual Basic famil

y

of languages

Supports object oriented features

Interpreted

Loosel

y

T

y

ped

(15)

Visual Basic & VBScript

Visual Basic

² Stand-alone (compiled

or interpreted)

² Variables are t

y

ped

VBScript

² Embedded in a

W

eb

page and executed b

y

browser

² Variables can be

associated wit

h

an

y

t

y

pe

² Uses most program

structures available in

VB, including loops

(16)

C

ontents

S yntax Rules Comments Naming Rules Data T ypes Storage

² Variables, Constants, Arrays

Operators

² Arithmetic, Comparison, Logical, Assignment, Concatenation

Branc hing ² If ² Select Looping ² While ² Do«While ² For Sub/Functions ² Built-In ² User defined

(17)

S

y

ntax Rules

VBScript is not case sensitive

² Except for strings (denoted b

y

double-quotes)

Eac

h

statement can onl

y

exist on one line

² Unless

y

ou use a line continuation

Statements can be eit

h

er simple statements or

(18)

C

omments

em

commented statement

commented statement

C

an be a complete line or a portion of a line.

VBScript does not support multi-line comments.

T

h

e comment s

y

mbol must be put at t

h

e start

(19)

Naming Rules

1. Must begin wit

h

a letter or t

h

e underscore

Good: count, strInfo, _data

Bad:

2

da

y

,

4

get, *count violate t

h

is rule.

2

.

C

annot contain special c

h

aracters except t

h

e

underscore

Good: Names using alp

h

abets, numbers and t

h

e underscore.

Bad: Names using s

y

mbols and ot

h

er special c

h

aracters.

3

.

C

annot matc

h

a reserved word

Bad: for, if, Select

4

. Must be unique wit

h

in a context

(20)

Naming

C

onvention

obj

² T

h

ree letter prefix for t

h

e data t

y

pe

DescriptiveName

² A descriptive name for t

h

e variable

² First letter of eac

h

word in upper case

(21)

Literal T

y

pes

Numeric

² 1

²

3

.1

4

²

3

E

7

Boolean

² True

² False

Ch

aracters

² ´Hello

W

orldµ

² ´1

23

Straw Laneµ

² ´

432

10µ

Date

² #0

7

/

2

1/

2

00

6

² #Jan

6

,

2

00

8

(22)

Variables

Dim

variableName

Variables

h

old values t

h

at can c

h

ange during

program execution

T

h

e

y

function just like temporar

y

storage

locations

Declaration of variables is optional, but

recommended

(23)

C

onstants

C

onst

constant

Name

=

value

C

onstant names

h

old values t

h

at

c

annot

c

h

ange

during program execution

C

onstants s

h

ould be used in place of

h

(24)

Operations

Operations are t

h

e man

y

forms of computations,

comparisons etc t

h

at can be performed in VB

Most operations are binar

y

in t

h

e form:

operand1 operator operand

2

Ot

h

er operations are unar

y

in t

h

e form

operator operand1

Wh

en an operator

h

as

2

s

y

mbols,

y

ou cannot separate

t

h

em wit

h

a space:

² Good:

2

<>

3

(25)

Arit

h

metic

Used to perform calculations

Bot

h

operands must be numeric values

T

h

e result is a numeric value

+

Addition

-

Subtraction, Negation

*

Multiplication

/

Division

\

Integer

Division

^

Exponent

Mod

Modulo

(26)

C

omparison

Used to compare t

h

e value of two items

Bot

h

operands must be of t

h

e same data t

y

pe

T

h

e result is a Boolean value

=

Equalit

y

<>

Inequalit

y

<

Less

t

h

an

>

Greater

t

h

an

<=

Less

t

h

an or equals

>=

Greater

t

h

an or equals

(27)

Logical

Logical

Used

Used to

to redu

reduce B

ce Bool

oolean

ean valu

values in

es into a

to a single

single Boo

Boolean

lean

value

value

B

Bo

ott

h

h

operands must be Boolean values

operands must be Boolean values

T

T

h

h

e result is a Boolean value

e result is a Boolean value

And

And

C

C

onjunction

onjunction

O

Orr

D

Diissjju

un

nccttiio

on

n

N

No

ott

N

Ne

egga

attiio

on

n

X

(28)

And Trut

And Trut

h

h

Table

Table

C

C

onjunction

onjunction

U

Usse

ed

d w

w

h

h

en bot

en bot

h

h

conditions are required

conditions are required

O

Op

pe

erra

an

nd

d1

1

O

Op

pe

erra

an

nd

d

2

2

Result

Result

T

T

T

T

T

T

T

T

F

F

F

F

F

F

T

T

F

F

F

F

F

F

F

F

(29)

Or Trut

Or Trut

h

h

Table

Table

D

Diissjju

un

nccttiio

on

n

U

Usse

ed

d w

w

h

h

en eit

en eit

h

h

er condition is sufficient

er condition is sufficient

O

Op

pe

erra

an

nd

d1

1

O

Op

pe

erra

an

nd

d

2

2

Result

Result

T

T

T

T

T

T

T

T

F

F

T

T

F

F

T

T

T

T

F

F

F

F

F

F

(30)

Not Trut

h

Table

Unar

y

Operand

Used to c

h

ange t

h

e value of a condition

Operand 1

Result

T

F

(31)

Xor Trut

h

Table

Exclusion

Used w

h

en eit

h

er condition s

h

ould be different

Operand1

Operand

2

Result

T

T

F

T

F

T

F

T

T

(32)

Assignment

Ch

anges t

h

e value of a variable

=

Uses t

h

e same s

y

mbol as an equalit

y

c

h

eck operator.

Assignment onl

y

occurs w

h

en used in an

y

of t

h

e

following forms:

variable = literal value

variable = expression

(33)

C

oncatenation

C

ombines

2

data t

y

pes for displa

y

as a string

(34)

Branc

h

ing

Allows

y

ou to avoid running certain sections of

y

our code

C

ode is onl

y

executed w

h

en a condition (or

conditions) evaluate to True

Provides a single application t

h

e abilit

y

to react

(35)

If

If 

cond ition

T

hen

statement( s)

End If 

Performs an operation onl

y

if t

h

e condition

evaluates to True

Used w

h

en an action is eit

h

er performed or not

(36)

If«Else

If 

cond ition

T

hen

statement( s)

Else

statement( s)

End If 

Performs t

h

e If portion onl

y

if t

h

e condition

evaluates to True and t

h

e Else portion ot

h

erwise.

Used in an eit

h

er or scenario w

h

en an operation

is alwa

y

s performed.

(37)

If«ElseIf

If cond ition1 Then statement( s) ElseIf cond ition2 Then statement( s) Else statement( s) End If 

Onl

y

one section can be executed even if man

y

conditions

evaluate to True.

Used in a multiple c

h

oice scenario

(38)

Select

Select Case variable

statement( s) Case value1 statement( s) Case value2 statement( s) Case Else statement( s) End Select

Used to c

h

oose between 1 of several discrete values for a

variable.

Inclusion of

C

ase Else

is optional

(39)

Loops

Allows

y

ou to repeat running certain sections of

y

our

code

C

ode executes w

h

en a condition (or conditions)

evaluate to True

Be careful wit

h

Loops. T

h

e

y

can result in infinite

processing.

Forms

² Entr

y C

ondition

Entr

y

onl

y

w

h

en a initial condition is met

² Iterated

(40)

Loops:

C

autionar

y

Items

1.

C

an t

h

is loop ever be entered

If no, t

h

en

y

ou don·t need t

h

e loop

2

.

C

an t

h

is loop ever be exited

(41)

Loop: Parts

1.

Wh

ere does t

h

e loop start?

Expressed as an assignment

2

.

Wh

en does t

h

e loop end?

Expressed as a condition

3

. How is t

h

e loop variable altered?

Expressed as an arit

h

metic operation

(42)

Wh

ile

W

hile

cond ition

statement( s)

W

end

Entr

y C

ondition Loop

Simplest form of t

h

e loop

Requires manual modification of t

h

e loop

(43)

For

F

or

variable

=

start

T

o

 finish [

S

tep

change]

statement( s)

N

ext

Iterated Loop

Favored because all t

h

e loop details are in t

h

e

(44)

Function

Fu

n

c

tion

 functionName(parameter list)

statement( s)

 functionName = value

End

Fu

n

c

tion

Block of code used to perform an operation

Bot

h

VBScript and QTP provide a large number of

built-in functions

(45)

Best Practices

C

omment liberall

y

Use a naming convention

Avoid mixing cases

(46)

You're Reading a Preview

Unlock full access with a free trial.

(47)

VBScript Examples

Greeting (printing to message box)

Greeting

2

(printing to current window)

Speed

Ch

eck (Dicision)

Blast Off! (FOR loop)

Var

y

ing Font (FOR loop)

Speed

Ch

eck

2

(event

h

andler)

Speed

Ch

eck

2

B (alternate code)

Get User Input

Heart Beat (calculation in event

h

andler)

Heart Beat

2

(arguments to event

h

andler)

(48)

Greeting

<html> <head> <title>VBScript Demo</title> </head> <body> <h1>VBScript Demo</h1> <script language="VBScript"> <!--Dim name Dim age name = "Jack" age = 20

MsgBox "Hello, " & name & ". Are you " _ & age & " years old?³

-->

</script> </html>

(49)

You're Reading a Preview

Unlock full access with a free trial.

(50)

You're Reading a Preview

Unlock full access with a free trial.

(51)

You're Reading a Preview

Unlock full access with a free trial.

(52)

Var

y

ing Font (For Loop)

...

<body>

<h1>For Loop Demo</h1>

<script language="VBScript"> Dim num

For num = 1 To 6

document.write("<font color='red' size=" & _ num & ">")

document.write("<i>This sentence was´ & _

³ generated by VBScript.</i>") document.write("</font><br>") Next </script> </html>

Try it.

(53)

Events

Event :

An action b

y

t

h

e user--e.g., mouse-click,

mouse-move, etc.³t

h

at triggers execution of a

code known as

event hand ler 

Event Handler :

Piece of programming code, written in

VBScript, Javascript, Java, etc., allowing t

h

e

(54)

Event Examples

Event Name

Event

Name

Example

Example

onClick 

onClick

Mouse button clicked on textbox

Mouse button clicked on textbox

onDblClick 

onDblClick

Button double--clicked on textbox

Button double

clicked on textbox

onMouseMove

onMouseMove

Mouse is moved

Mouse is moved

onMouseOut

onMouseOut

Mouse pointer leaves image area

Mouse pointer leaves image area

onMouseOver

onMouseOver

Mouse pointer first enters image area

Mouse pointer first enters image area

onLoad

(55)

You're Reading a Preview

Unlock full access with a free trial.

(56)

Subprogram

G

reeting

<html>

<head>

<script language="VBScript">

<!--Sub Greetings()

MsgBox "Welcome, " & theForm.myName.value

End Sub

-->

</script>

</head>

<body>

«

</body>

</html>

(57)

You're Reading a Preview

Unlock full access with a free trial.

(58)

You're Reading a Preview

Unlock full access with a free trial.

(59)

Speed

Ch

eck

2

(Event Handler)

<script language="VBScript"> <!--Sub CheckSpeed() Dim yourSpeed Dim diff  Dim message const maxSpeed = 50 yourSpeed = 60

message = "Your Speed is " & yourSpeed & " mph." diff = yourSpeed - maxSpeed

If diff > 0 Then

message = message & " You are speeding by " & diff & " mph." Else

message = message & "You are under the speed limit." End If 

MsgBox message End Sub

-->

(60)

Speed

Ch

eck

2

B (Alternate

C

ode)

<html> <head> <title>VBScript Demo</title> <script language="VBScript">

<!--I nsert Alternate code for event procedure here. --> </script> </head> <body> <h1>Speed Check</h1> <form name="myForm">

<input type="button" name="btnStart" value="Click Me"> </form>

</body> </html>

(61)

Get User Input

<html> <head>

<script language=³VBScript´>

. . . C ode for GreetUser() goes here

</script> </head> <body>

<h1>Get User Information Demo</h1> <form name="myForm">

Please enter your name.

<input type="text" name="myName" size="10"><br> Please enter your age.

<input type="text" name="myAge" size="3"><br> <p>

<input type="button" name="start" value="Click Me" onClick="GreetUser()">

</form> </body> </html>

(62)

You're Reading a Preview

Unlock full access with a free trial.

(63)

You're Reading a Preview

Unlock full access with a free trial.

(64)

GreetUser() Version

3

<script language=³VBScript> <!--Option Explicit Sub GreetUser() Dim message

message = "Welcome to the world of VBScript, " & _ myForm.myName.value & "!"

message = message & " Are you really " & _ myForm.myAge.value & " years old?"

document.write("<h1><font color='blue'>Welcome ³ & _ Message</font></h1>")

document.write(message) End Sub

- ->

(65)

You're Reading a Preview

Unlock full access with a free trial.

(66)

You're Reading a Preview

Unlock full access with a free trial.

(67)

You're Reading a Preview

Unlock full access with a free trial.

(68)

You're Reading a Preview

Unlock full access with a free trial.

(69)

You're Reading a Preview

Unlock full access with a free trial.

(70)

You're Reading a Preview

Unlock full access with a free trial.

(71)

You're Reading a Preview

Unlock full access with a free trial.

(72)

You're Reading a Preview

Unlock full access with a free trial.

(73)

You're Reading a Preview

Unlock full access with a free trial.

References

Related documents

Participants, while interacting with a small red sphere, delayed their action response than during other interactions of global (i.e., a cylinder) or local motion cues (i.e.,

As our Broadband and Voice Networks are directly connected your voice traffic will automatically take the quickest path to our network, ensuring optimal call quality and the

12.1.) Notwithstanding delivery and the passing of risks in the goods, or any other provision of these conditions, the Seller retains title to all delivery items until the Buyer

Postoje određeni uslovi koje morate da poštujete kada odlučite da razvijate vidovitost. Prva stvar je stanje vašeg uma kojim počinjete rad.. zno da verujete u sve motive i

Carotid body tumors present as a palpable mass in anterolateral aspect of the neck causing splaying of the external carotid artery (ECA) and internal carotid artery (ICA).. CBTs

the present study has two main objectives: (1) to test the effects of different categories of valence (pleasant; unpleasant) and arousal (high; low) on two event-related

Your computer will require either a USB port (if you have a USB modem) or a network card with an Ethernet port (if you have an Ethernet or a Wi-Fi router). Most new laptops and PC’s

And yet, as a community of faith, we know we’ve been through challenging days before and can continue to walk together with faith, hope and love as those who have heard the words