Embed
Email

Client side programming with JavaScript and DHTML

Document Sample

Shared by: ewghwehws
Categories
Tags
Stats
views:
0
posted:
1/23/2012
language:
pages:
44
Client side programming with

JavaScript and DHTML

Dr Jim Briggs

What is JavaScript?

• Scripting Language • JavaScript is lines of

• Developed by code combined with

Netscape in 1995 HTML

• Originally called – JavaScript is

LiveScript supported by all major

• Renamed to form a browsers

marketing relationship • (but in slightly different

ways!)

with Java

– The client has the

• An open language - option not to run

no licence required JavaScript

2

What can JavaScript do?

• JavaScript introduces interactivity to web pages

– This is achieved by controlling the browser

• Common uses for JavaScript:

– Validating forms

– Performing calculations

– Animating images, including rollovers

– Creating advertising banners

– Detect certain predetermined aspects of the environment

• e.g. the browser version or required plug-ins

– Generate date and time features including calendars and time

stamps





3

Tools for the trade

• Any simple text editor

– Textpad

– Notepad

• Specialist HTML editors

– Dreamweaver

• Browsers (for testing and debugging)







4

How to run JavaScript







document.write(“hello World”)









5

JavaScript the Output









6

JavaScript's Compatibility

• Some very old browsers don’t support

JavaScript

– Netscape 1.X and Internet Explorer 3

• Some implement it in different ways

– Netscape vs. Internet Explorer

• Getting better!







7

JavaScript's Compatibility

• To insert HTML that only appears in non-

scripting browsers



• This would also be visible to those that

have JavaScript turned off









8

Animation

What is a rollover?

• As the user moves the mouse over an

image or text the view changes

• It is a reaction to an event

• It could invoke a status message or

change the colour of a graphic on the web

page







10

Example 1



Place your mouse here!



• Example: rollover1.html







11

Example 2 code



Place your mouse here!



• rollover2.html



12

Example 3

• Use the same method to swap one image

for another.

• rollover3.html









13

Multiple Links

• It is easy to have multiple rollovers in a

page

• buttonover.html









14

Example 4 - Cycling Banners

• One feature of JavaScript is its capability to

dynamically change images in web pages

• This technique is usually incorporated into

adverts that popup on your web page

• JavaScript can do this very simply by using an

image array

• The code cycles through a selection of graphics,

giving the impression of the graphics rotating

• This creates dynamically changing content on

the page without the need to use frames and

layers

15

Cycling Banners

• Disadvantages:

– Only images that exist in the document can

change

– New one can’t be added

– Existing ones can’t be removed

• Images with differing sizes can still be

used

• Image download times

16

Cycling Banners

• Image arrays

• Array contains each of the images

required for the page

• The image then becomes the child object

• rotbanner.htm









17

Slide Shows

• Here the user controls the flow of the

images

• This works simply by using the array

element numbers

• Add 1 to move forward

• Subtract 1 to move backwards

• Add the navigation to the page

• slideshow.htm



18

Form validation

Form validation with HTML

• Forms are used frequently in websites to

collect data

• They incorporate a variety of graphical

interface elements, e.g. Radio Buttons,

Check Boxes, Pop-up Menus and textual

entry fields







20

Form validation with HTML

• User inputs the data

• Once completed the Submit button is

clicked

• This triggers an event - sending the data

to the web server

• Here the server-side program interprets

the data and acts on it



21

Form validation with HTML

• Disadvantage: • The solution:

– The user has to – To validate on the

complete the whole client-side

form and submit it – JavaScript enables

before the data gets validation on the

validated client-side

– The data has to reach

the server before the

validation can

commence





22

Form Validation using

JavaScript

• Example

• When changing a password the user is asked to

enter the new password twice to check for typos

• If the text fields do not match an Alert box will

appear to inform the user

• If the password input is accurate then it is sent to

the server

• password_check.html





23

Active site navigation

• With JavaScript you can create a select

and go menu to select options from a drop

down list in one click, eliminating the go

button

• Take care not to confuse people who have

JavaScript turned off!







24

Active site navigation

• When the menu is not pulled down, it will

display a user prompt

• This technique is used by many websites

to move the user to different sections

• SelectAndGo.html









25

Validating fields

• Use Regular Expressions

– Powerful way to validate and format text

strings

– It provides compact way of coding

– You need to know the syntax

• There is only a limited amount of checking

that can be done client-side

• RegExp.html



26

Regular Expressions

• This regular expression will test the e-mail

address for us:

re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

• re is the variable the RegExp is assigned

to

• RegExp begins and ends with /

• ^ indicates beginning of string; $ end

• \w is any character a-z, A-Z and 0-9

• + one or more of the previous item

27

Regular Expression

• The use of () brackets indicate • + must exist 1 or more times

a group e.g. .com, .ac.uk, .net

• Square brackets [] indicate • * after an item indicates 0 or

any one of the characters more of the item

inside • @ represents the @ character

• \. means a full stop (. means • \w+ means 1 or more

any character) characters

• ? means optional • ([\.-]?\w+)* indicates a . or –

• \.\w{2,3} looking for the . are allowed in the suffix/prefix

followed by either 2 or 3 of an e-mail address

characters









28

Event handling

Event Handling

• Events are actions that the user performs

while visiting your web page

• These events can be triggered by:

– A mouse click

– A key press

– Form loading









30

The onLoad Event

• Triggered when the user enters your page and

all its elements have loaded

– A common use of this event is to have advertisement

windows pop up when you first load the page

• The onLoad command is normally in the

tag.



• If you want to do more than one thing, define a

function





31

onUnLoad Event

• Triggered when the user leaves the site

• Used commonly again for pop up advertising

windows when you leave the site.

• Can be used to produce an infinite loop of pop

up’s - a user’s nightmare!

• A possible use would be to display an exit

message.

• The onUnLoad is normally on the tag,

similar to the onLoad command.





32

Mouse Event Handlers

• Commands can be • onmousedown

associated with most • onmouseup

page elements • onclick

• Event only applies to • ondblclick

element it is

associated with • onmouseover

• onmouseout

• onmousemove





33

onMousemove Event

• This is triggered when the user moves the

mouse over an element

• The eyes









34

onMouseover Event

• This command is used extensively with

image rollovers.

• This event is triggered when the mouse is

moved into any area for which the

command has been assigned (i.e. over an

element)







35

onMouseout Event

• The reverse of the onMouseover.

• Triggered when the user moves the

mouse out of the area for which the event

has been assigned.









36

Form event handling

• Form handling event are largely used in form validation

scripts

• onSubmit event

– Triggered when the user clicks on the Submit button to complete

the form

– If the result of the handler is false then the form is not sent to the

server.

• onReset Event

– Triggered when the user clicks the reset button on the form

– A function should be called to (re-)assign the default values to

the form.





37

Form Event handling

• onSelect Event

– Is trigger if the user selects text in an INPUT

or a TEXTAREA form field

• onChange Event

– Triggered when a user changes a form field.

– Used to verify information immediately or to

respond to a user’s choice before the submit

button is clicked.



38

Advanced interfaces

User Interface Design

• "MS-Windows" interface elements not

available in HTML:

– Menu bars

– Pull-down menus

• Need to accomplish with JavaScript









40

Pull down menus

• Source: pulldownmenu.html

• The function toggleMenu() switches

between the menu pulled down and not

– currElem is the element to be toggled

– nextPos is where you want the top position of

the menu to be









41

Sliding menus



• DHTML enables elements of a page to be

switched on or off

– Not implemented in Netscape 4

– display attribute: either "none" or "block"

• Source slidingmenu.html









42

Tool Tips

• Tool tips appear when you hold a cursor over an

item.

• Example: tooltips.html

• Contains an image map (… tag)

– Associate named map to image (

attribute)

– Specify region of an image ( tag)

• rect, circle or poly

– Associate action with that region

• Can be client-side action (e.g. JavaScript or follow link) or

server-side (send co-ordinates to server)



43

Click anywhere form fields

• In MS-Windows, clicking on the label of a

control selects it

• Browser interaction doesn’t work in the

same way - the control requires a precise

click

• Source: clickanywhere.html







44



Related docs
Other docs by ewghwehws
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!