Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Javascript and HTML - Lecture Slides | IT 3203, Study notes of Information Technology

Material Type: Notes; Professor: Brown; Class: Intro to Web Development; Subject: Information Technology; University: Southern Polytechnic State University; Term: Fall 2006;

Typology: Study notes

Pre 2010

Uploaded on 08/03/2009

koofers-user-6g9
koofers-user-6g9 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
IT 3204
Introduction to Web Development
JavaScript and HTML
September 27
Syllabus Error Corrected
The first exam is October 2. Please correct
your paper syllabus.
This error has been corrected in the on-line
syllabus.
Labeling Systems
Feedback in a conversation is immediate;
Feedback when we “converse” via a Web site is
at best delayed.
What we call things (labels) are an important part
of the conversation.
Here is a Web Page
Kinds of Labels
Navigation system choices
Contextual links
Headings
Index terms
Labels Within Navigation Systems
MIND
& SPIRIT
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Javascript and HTML - Lecture Slides | IT 3203 and more Study notes Information Technology in PDF only on Docsity!

IT 3204

Introduction to Web Development

JavaScript and HTML

September 27

Syllabus Error Corrected

  • The first exam is October 2. Please correct

your paper syllabus.

  • This error has been corrected in the on-line

syllabus.

Labeling Systems

Feedback in a conversation is immediate;

Feedback when we “converse” via a Web site is

at best delayed.

What we call things (labels) are an important part

of the conversation.

Here is a Web Page

Kinds of Labels

  • Navigation system choices
  • Contextual links
  • Headings
  • Index terms

Labels Within Navigation Systems

MIND

& SPIRIT

Labels Within Navigation Systems Labels as Contextual Links

Labels as Headings Labels as Index Items

About Iconic Labels How Jet Blue Fixed It

A Simple JavaScript Example

Test of JavaScript

A Simple JavaScript Example

var i=5; document.write("

"); document.write("

"); document.write(""); document.write("
Number SquareCube
"+i+" "+i*i+""+i*i*i+"
");

Result of the Example

A two-row table with headings and data

The Document Object

  • The “document” object is a container for the
displayed HTML; it is a property of “window”
  • The document object has properties and methods
  • One of the methods is the “write( )” method, which
sends HTML to the browser’s rendering engine.
  • The HTML elements displayed by the browser are
properties of the document object.

The Document Object Model

  • Defines an interface between HTML and applications
  • DOM 0 Not a standard…
ad hoc model for early support of JavaScript
  • DOM 1 HTML and XML; 1998
  • DOM 2 Added style sheet support; 2000
Netscape 6, not complete in IE 6
  • DOM 3 XML content; future

An HTML Document is a Tree

head

html

body

title h1 p

The DOM Interface

  • An abstract interface
  • Logically, a tree-like structure
(not necessarily implemented as a tree)
  • There must be a correspondence (binding) between
language and model
  • There can be more than one tree in a document
  • DOM objects have properties and methods.

The DOM Tree

DOM Example

One object, two properties

type value “text”

name value “address”

(name= is deprecated in XHTML. Use id= instead

of name other than in form elements. If id and name

are both present, they should generally be the same.)

DOM Objects Contain Arrays

  • HTML elements are usually not required to have

an ID attribute.

  • The DOM uses arrays to make anonymous

elements accessible to JavaScript.

  • The document object contains a forms[ ] array.
  • The forms[ ] array contains an elements[ ] array.

DOM Example

document.forms[0].elements[0]

What about introduction of new elements?

Name: Address: City:

Name: Address:

City:

Identifying Elements

XHTML Strict does not allow name= on (and

you don’t need it, either!)

You do need name= on and example shows

the name and ID attributes as identical

Event Handlers for Buttons

onclick Occurs when button is clicked
Also checkboxes and radio buttons

Radio buttons: separate event handlers for each button,

or loop through looking for checked=true.

Radio Button Example

var elements=document.getElementById ("myForm").elements; // all elements

for (var i=0;i<elements.length;i++){ if (elements[i].checked) { result=elements[i].value; } }

Radio Buttons – Checking One Group


var x=document.getElementById("order").elements; for (var i=0;i<x.length;i++){ if ((x[i].name == "credit") && (x[i].checked)) { selected=x[i].value; } }

Radio Buttons – Checking One Group

Validating Form Input

Why check on client?
  • fast; no network access
  • immediate feedback
Why check on server?
  • secure
  • can do database lookups
Which way is right?

Validation Example

Phone:

Phone:

The “verify” Function

function verify( ) { var hasError = false; var phNum=document.getElementById("phone").value; var ok = phNum.search(/^\d{3}-\d{3}-\d{4}$/); if (ok !== 0) { hasError = true; alert("Invalid phone number."); } if (hasError) return false; else return true; }

The HTML Document

Verify Tester
Phone:

Steps to Client-Side Validation

Verify, field by field, setting a flag.
alert( ) message ... clear, complete and polite...
Focus on erroneous element:

err=document.getElementById( " phone " ); err.focus();

Select the text, possibly:

err.select();

Return false to stop form submission

This works when the button is clicked, but not if the form

is submitted another way, such as with the submit( )

method of the form object:

This always works, and so is the preferred method:

Where to Call verify( )

Comparing Passwords

Scenario: Routine to change passwords

We want the user to type it twice

Algorithm:

1st password blank? error

1 and 2 not equal? error

otherwise, return true

Registering the Event Handler

document.passForm.onsubmit = chkPass;

What's the other way?
Why is this placed after ?
Why not chkPass( );?

DOM 2 Event Propagation

An event occurs. The event starts at “document,”

propagates down through the DOM tree

At each level, if handlers exist, they are run

A handler can “capture” the event.

The handler on the “target” is run if it exists

Some, but not all, events “bubble” back up.

DOM 2 Event Registration

Call addEventListener with:

event name

handler function name

Boolean: call only during capture phase

document.order.phone.addEventListener ("change", chkPhone, false);

If true, called only during capture phase; if false, can

be called at target node or during bubbling.

Stopping Propagation

stopPropagation method of event

document.order.phone.change.stopPropagation();

Can be applied during “downward” propagation or during

bubbling.

Do this when the event has been completely handled.

Stopping Default Action

In DOM 0, return false
In DOM 2, preventDefault method

document.order.submit.preventDefault();

DOM 2 Reminder

The DOM 2 event model is very powerful
It doesn’t work on IE
(because MS didn’t implement it, that’s why!)
Therefore, it’s mostly useless today.

The Navigator Object

Provides information about the browser

var browser = "Browser Information:\n"; for (var propname in navigator) { browser += propname + ": " + navigator[propname] + "\n" } alert(browser);

The Navigator Object

Don’t count on all the

information: MSIE

claims to be Mozilla!