Top Banner
JavaScript IV ECT 270 Robin Burke
22

JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Jan 03, 2016

Download

Documents

Peregrine Hardy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

JavaScript IV

ECT 270

Robin Burke

Page 2: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Outline

DOMJS document model reviewW3C DOM

Page 3: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

JS Document Model

Collection-baseddocument.formsdocument.imagesdocument.all

Name-baseddocument.forms[0].total

Page 4: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Modifying the document

Changing attributeslike img srclike color rollover

Some parts of the document not so easy to accessespecially textual document content

Not possible to build an HTML document within JS

Page 5: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Example

Modifying document contentcolor rolloveradd menu item

Page 6: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

What we can't do

Can't access textual content Can't build an HTML document

Page 7: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

W3C DOM

Document Object Model Based on the DOM for XML

not (very) HTML-specific Much more flexible

can build documents can access any part of the document

Levels 1 – Basic standard 2 – CSS / events

Page 8: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

HTML

<html><head><title>DOM Example</title></head><body><h1>DOM Example</h1><div name="pict" style="text-align: center; border-

width: 3 px; border-style: double"><img name="stickers" src="../w7/img/stickerface.gif"

width="230" height="238"> </div><p>Some text and <a href="lec1110.html">a link to the

lecture</a>. End of page.</p></body></html>

Page 9: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Internal representation

HTML

HEAD

TITLE

(text)

BODY

H1

(text)

DIV

IM

A

P

(text) (text)

(text)

Page 10: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Access via JS Document document links [0]

images [0]all

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

pict

stickers

HTML

HEAD

TITLE

(text)

BODY

H1

(text)

DIV

IM

A

P

(text) (text)

(text)

Page 11: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Problem

This is a mess new collections added for every purpose some collections browser-specific

W3C solution methods for traversal of the tree no special collections ability to generate collections

• based on tag name• based on id

Page 12: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

W3C DOM

Basic pieces Node

• general type NodeList

• wherever a collection is needed Element

• HTML element• subtype of Node

Text• a subtype of Node• contains only unmarked-up character data

Page 13: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Accessing DOM contents

document methods getElementsByTagName

• collected by tag (img, a, div, etc.) getElementById

• must be labeled by id, not name node methods

parentNode childNodes firstChild lastChild

element methods getAttribute setAttribute

Page 14: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Modifying document content

factory methods of documentcreateElement (tagName)createTextNode

node modifiersappendChild (node)removeChild (node)insertBefore (newNode, currentNode)replaceChild (newNode, oldNode)

Page 15: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Example

add menu item add textual content

Page 16: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Style

What if we want to mark missing fields in red?

DOM solutionadd a new span node with red color

Another solutionuse style

Page 17: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Manipulating style

We can manipulate style dynamicallyjust like other element properties

Each element has an associated style objectsetting the properties of this objectchange the element's displayed styleediting embedded style

Page 18: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Note

CSS "-" is style name separator font-family: Arial, sans-serif

JavaScript "-" is subtraction operator style names use lowerUpper syntax

• font-family becomes fontFamily• elem.style.fontFamily = "Arial, sans-serif"

Netscape style property is missing from "schismatic" Netscape

versions (4-5) instead

• elem.fontFamily = ...

Page 19: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Cross-browser solution

StyleAPI fileif (document.layers) isNS = true;

if (document.all) isIE = true;

function setFontFamily (elem, family)

{

if (isIE)

elem.style.fontFamily = family;

else if (isNS)

elem.fontFamily = family;

}

Page 20: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Examples

text color rollover change style of label if we just want the asterisk red

must insert a span anyway

Page 21: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Summary

Pluses Available in both NS and IE

• not true of JS document Conceptually simpler More capable

Minuses Code is wordier Implementation differences in browsers Browser features not standardized in NS 4

and 5

Page 22: JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.

Wednesday

More Styleespecially positioningspecial effects