Title: Web Programming Overview
1Web Programming Overview
- HTML
- Hypertext Markup Language
- DHTML
- Dynamic Hypertext Markup Language
- Any kind of coding that changes the behavior of a
web page based on user response - Client-side programming run in client browser
- Server-side programming run on web server
- XML
- EXtensible Markup Language
- Data and data formatting standard
2Client-Side Programming
- Written in Javascript,VB Script or other
languages - Fast because doesnt have to communicate with
server - Often used for data validation or data formatting
- Generally less secure than server-side
- Demo
- http//www.purdue.anderson.edu/Cpt/Courses/Cpt255/
Quiz.htm - View source to see Javascript
3What is VBScript?
- VBScript (also known as Visual Basic Scripting
Edition) is a lightweight programming language
that is designed to run inside web pages and
other applications. - VBScript can be used for both client-side and
server-side programming. - VBScript is a scaled down version of Visual
Basic. - Visual Basic for Applications (VBA) - a subset of
Visual Basic that is optimized for use with
Microsoft Access and other Microsoft Office
applications. - VBScript is optimized for use in web browsers,
especially Internet Explorer.
4Placing Client-side Vbscript In Web Pages
- Script Blocks
- Runat"client" optional but best practice
- Can place blocks in body to be run as page is
displayed - Can place blocks in head functions and sub
procedures
ltSCRIPT LANGUAGE"VBScript" runat"client"gt
Option Explicit Public Sub WriteALine(What2Write
) Document.Write(What2Write) Document.write("lt
BR /gt") End Sub lt/scriptgt
example1.htm
5VBScript Variables
- A memory location holding data that can be
accessed or changed at a later time while the
program is running - All variables are variant datatype
- hold either text or numbers
- Behave as a number when you use them in a numeric
context and as text when you use them in a text
context
6VBScript Variables (cont)
- Do not have to be explicitly declared
- Unless use Option Explicit
- But requiring explicit declaration is best
practice - Declare variables with Dim, Private, or Public
followed by the variable name - Public variables are available to all procedures
in all scripts in current page - Private variables are available only to the
script in which they are declared - Dim variables
- (at script level) are available everywhere within
the script - (at procedure level) are available only within
procedure
7VBScript Variables (cont)
- Naming
- Must begin with an alphabetic character
- Cannot contain a period
- Cannot exceed 255 characters
- Must be unique within its scope
8VBScript Arrays
- An array is a set of variables conveniently
packages for easy handling - Dim Emp(5)
- One dimensional
- Dim Emp (5,3)
- Two dimensional
- Dim Emp()ReDim Emp(x)
- Dynamic dimensioning
9VBScript Constants
- Similar to a variable except that its value never
changes - Usefulness
- In reviewing code a constant name can be more
meaningful than the number which is its value - The value of the constant can be changed in one
place - Declaration
- Const cSalesTax .05
10Select Case
Syntax Select Case testexpression Case
expressionlist-n statements-n . . .
Case Else expressionlist-n
elsestatements-n End Select Example Select
Case grade Case "A" Document.Write("Excellent")
Case "B" Document.Write("Good") End Select
example2.htm
11If-Then-Else
Syntax If condition Then statements ElseIf
condition-n Then elseifstatements . .
. Else elsestatements End If Example If
grade gt 89 Then Document.Write("Excellent") Else
Document.Write("Good") End If
12Do Loop
Syntax Do While Until condition
statements Exit Do Loop Examples poweroftw
o 2 Do While poweroftwo lt1000 Document.Write(po
weroftwo) Document.Write("ltBR /gt") poweroftwo
poweroftwo 2 Loop
poweroftwo 2 Do Document.Write(poweroftwo
) Document.Write("ltBR /gt") poweroftwo
poweroftwo 2 Loop While poweroftwo lt1000
With Do Loop While Until, loop will be
processed at least once
13For-Next Loop
Syntax For counter Start To End Step
stepvalue statements Exit
For Next Examples For i 1 to
10 Document.Write(i) Document.Write("ltBR
/gt") Next
14For Each -Next Loop
Syntax For Each element In group statements
Exit For Next element Examples For Each
Item In Document.MyForm.Elements Document.Write(I
tem.Name) Document.write(" ") Document.Write(I
tem.Value) Document.write("ltBR /gt") Next
15Selected Built-in Formatting Functions
16Selected Built-in Math Functions
17Selected Built-in String Functions
18Selected Built-in Date Functions
19Date FunctionTime Intervals
20Selected Built-in Data Conversion Functions
21Selected Built-in Misc Functions
22Calling Built-in Functions
ltSCRIPT LANGUAGE"VBScript" runat"client"gt
Option Explicit Dim currentdate currentdate
Now Document.Write("The current date and time is
") Document.Write(currentdate) Document.Write("ltbr
/gt") Document.Write ("The current date is
") Document.Write(FormatDateTime(currentdate,
vbShortDate)) Document.Write("ltbr
/gt") Document.Write ("The current (24 hour) time
is ") Document.Write(FormatDateTime(currentdate,
vbShortTime)) Document.Write("ltbr
/gt") Document.Write("This time next week, it will
be ") Document.Write(DateAdd("d",7,currentdate))
Document.Write("ltbr /gt") lt/scriptgt
See example1.htm
23Sub Procedures
Syntax Public Default Private SubName
(arguments) statements Exit Sub
statements End Sub Example Public Sub
WriteALine(What2Write) Document.Write(What2Write
) Document.write("ltBR /gt") End Sub Example of
Calling a Sub Call SubName(argument1,
argument2) SubName argument1, argument2
24User-written Functions
Syntax Public Default Private FunctionName
(arguments) statements FunctionName
expression Exit Function statements End
Sub Example Public Function Square(Number)
Square Number Number End Sub Example of
Calling a Function x Square(13)
Document.Write(Square(13))
25Passing Variables ByVal and ByRef
- ByVal (default) indicates that the value of the
variable is passed but not the variable itself.
The sub or function works with a copy of the
variable. - ByRef indicates that the variable itself is
passed and that changes made to the value of the
variable inside the sub or function will also
change the value of the variable outside the
routine.
26ByRef vs. ByVal
Private Sub ChangeValues(ByVal X, ByRef
Y) xx2 yy2 End Sub
ltbodygt ltSCRIPT LANGUAGE"VBScript"
runat"client"gt Option Explicit Dim x,
y x10 y20 Call ChangeValues(x,y) Document.Write(
x "ltBR /gt") Document.Write(y "ltBR
/gt") lt/scriptgt lt/bodygt
27Client-side VBScript Form Validation
- User entries on forms can be validated on either
client-side or server-side - Client-side validation is more immediate
- Client-side JavaScript is more universal
- Client-side VBScript uses Form_OnSubmit function
- Automatically called
- Sets function to True or False
- False cancels the form action
28Client-side VBScript Form Validation
ltSCRIPT LANGUAGE"VBScript" runat"client"gt
Option Explicit Private Function
MyForm_OnSubmit MyForm_OnSubmit True If
(Document.MyForm.First.Value) "" Then
MsgBox("First Name field blank. Please enter your
first name.") MyForm_OnSubmit False End
If If IsNumeric(Document.MyForm.First.Value)
True Then MsgBox("You typed a number. Please
enter your first name.") MyForm_OnSubmit
False End If If MyForm_OnSubmit True
Then MsgBox("Hello, " Document.MyForm.First.Val
ue) End If End Function lt/scriptgt
Example3.htm
29Practice
- In Class Workshop
- Review Exercise 3 (exercise3.htm)
- Write code for Exercise 1
- Assignment
- Write code for Exercise 4
- Due Sept 3
- Also, read Chapter 13 14