Upload
animatoro
View
219
Download
0
Embed Size (px)
Citation preview
8/4/2019 Chapter5 New [EDocFind.com][1]
1/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Chapter 5
Host Objects:
Browsers and the DOM
WEB TECHNOLOGIES
A COMPUTER SCIENCE PERSPECTIVE
JEFFREY C. JACKSON
1
8/4/2019 Chapter5 New [EDocFind.com][1]
2/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Overview
The Document Object Model (DOM) is an API
that allows programs to interact with HTML (or
XML) documents
In typical browsers, the JavaScript version of the APIis provided via the document host object
W3C recommendations define standard DOM
Several other browser host objects are informal,
de facto standards
alert, prompt are examples
Many others are covered in this chapter
2
8/4/2019 Chapter5 New [EDocFind.com][1]
3/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Example: Rollover effect
Cursor not over image Image changes when cursor
moves over
3
8/4/2019 Chapter5 New [EDocFind.com][1]
4/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
4
8/4/2019 Chapter5 New [EDocFind.com][1]
5/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Import
JavaScriptcode
5
8/4/2019 Chapter5 New [EDocFind.com][1]
6/60
8/4/2019 Chapter5 New [EDocFind.com][1]
7/60
8/4/2019 Chapter5 New [EDocFind.com][1]
8/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Notice that id of image is first argument to show()
8
8/4/2019 Chapter5 New [EDocFind.com][1]
9/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
9
8/4/2019 Chapter5 New [EDocFind.com][1]
10/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
DOM method returning Object
10
8/4/2019 Chapter5 New [EDocFind.com][1]
11/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Returns instance
ofElement(DOM-defined
host object)
representing
HTML element
with given id
11
8/4/2019 Chapter5 New [EDocFind.com][1]
12/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Some properties of
Element instance
correspond
to attributes ofHTML element
12
8/4/2019 Chapter5 New [EDocFind.com][1]
13/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Method inherited by Element instancesfor setting value of an attribute
13
8/4/2019 Chapter5 New [EDocFind.com][1]
14/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Effect: src attribute of HTML element withspecified eltId is changed to specified URL
14
8/4/2019 Chapter5 New [EDocFind.com][1]
15/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Introduction
Image src changed toCFP22.png when mouseis over image,
CFP2.png when leaves
15
8/4/2019 Chapter5 New [EDocFind.com][1]
16/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM History and Levels
Very simple DOM was part of Netscape
2.0
Starting with Netscape 4.0 and IE 4.0,browser DOM APIs diverged significantly
W3C responded quickly with DOM Level 1
(Oct 1998) and subsequently DOM Level 2
This chapter: JavaScript API forDOM2 +
some coverage of browser specifics
16
8/4/2019 Chapter5 New [EDocFind.com][1]
17/60
8/4/2019 Chapter5 New [EDocFind.com][1]
18/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Intrinsic Event Handling
18
8/4/2019 Chapter5 New [EDocFind.com][1]
19/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Intrinsic Event Handling
19
8/4/2019 Chapter5 New [EDocFind.com][1]
20/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Intrinsic Event Handling
20
8/4/2019 Chapter5 New [EDocFind.com][1]
21/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Intrinsic Event Handling
Intrinsic event attribute value is a script;
what language is it written in?
HTTP Content-Script-Type header fieldspecifies default scripting language
meta element allows document to specifyvalues as if they were header fields
Header field name Header field value
21
8/4/2019 Chapter5 New [EDocFind.com][1]
22/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Change
background color
of element
containing cursor
22
8/4/2019 Chapter5 New [EDocFind.com][1]
23/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
23
8/4/2019 Chapter5 New [EDocFind.com][1]
24/60
8/4/2019 Chapter5 New [EDocFind.com][1]
25/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Reference to Element instancerepresenting the td element
25
8/4/2019 Chapter5 New [EDocFind.com][1]
26/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
26
8/4/2019 Chapter5 New [EDocFind.com][1]
27/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Reference to Element instance
27
8/4/2019 Chapter5 New [EDocFind.com][1]
28/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
All Element instances have a style propertywith an Object value
28
8/4/2019 Chapter5 New [EDocFind.com][1]
29/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Properties ofstyle objectcorrespond to CSS style properties of
the corresponding HTML element.
29
8/4/2019 Chapter5 New [EDocFind.com][1]
30/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Rules for forming style property namesfrom names of CSS style properties:
If the CSS property name contains no
hyphens, then the style objects propertyname is the same
Ex: color color
Otherwise, all hyphens are removed and the
letters that immediately followed hyphens arecapitalized
Ex: background-color backgroundColor
30
8/4/2019 Chapter5 New [EDocFind.com][1]
31/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Net effect: silver becomes the specified value forCSS background-color property oftd element;browser immediately modifies the window.
31
8/4/2019 Chapter5 New [EDocFind.com][1]
32/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Alternative syntax (not supported in IE6):
32
8/4/2019 Chapter5 New [EDocFind.com][1]
33/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Alternate syntax (not supported in IE6):
Every DOM2-compliant style objecthas a setProperty() method
33
8/4/2019 Chapter5 New [EDocFind.com][1]
34/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Alternate syntax (not supported in IE6):
CSS property
name
(unmodified)
CSS property
value
Empty string
or
important
34
8/4/2019 Chapter5 New [EDocFind.com][1]
35/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Modifying Element Style
Advantages ofsetProperty() syntax:
Makes it clear that a CSS property is being
set rather than merely a property of the
style object
Allows CSS property names to be used as-is
rather than requiring modification (which can
potentially cause confusion)
35
8/4/2019 Chapter5 New [EDocFind.com][1]
36/60
8/4/2019 Chapter5 New [EDocFind.com][1]
37/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree
Recall that HTML document elements
form a tree structure, e.g.,
DOM allows scripts to access and modifythe document tree
37
8/4/2019 Chapter5 New [EDocFind.com][1]
38/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
There are many types of nodes in the
DOM document tree, representing
elements, text, comments, the document
type declaration, etc.
Every Object in the DOM document tree
has properties and methods defined by the
Node host object
38
8/4/2019 Chapter5 New [EDocFind.com][1]
39/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
39
8/4/2019 Chapter5 New [EDocFind.com][1]
40/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
40
8/4/2019 Chapter5 New [EDocFind.com][1]
41/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
41
8/4/2019 Chapter5 New [EDocFind.com][1]
42/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
42
8/4/2019 Chapter5 New [EDocFind.com][1]
43/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
Example HTML document
Function we will write that will
use Node methods and propertiesto produce string representing
Element tree
43
8/4/2019 Chapter5 New [EDocFind.com][1]
44/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
String produced by TreeOutline():
44
8/4/2019 Chapter5 New [EDocFind.com][1]
45/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
Example: walking the tree of an HTML
document
Reference to html element is contained indocumentElement property ofdocumentobject
Use Node-defined methods to recursively
create an outline ofnodeNa
mes: Depth in tree
45
8/4/2019 Chapter5 New [EDocFind.com][1]
46/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
46
8/4/2019 Chapter5 New [EDocFind.com][1]
47/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
Document Tree: Node
Contains nodeType value representing Element
47
8/4/2019 Chapter5 New [EDocFind.com][1]
48/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
Note: IE6 has a different event model
Event instance created for each event
Event instance properties: type: name of event (click, mouseover, etc.)
target: Node corresponding to documentelement that generated the event (e.g.,
button element for click, img formouseover). This is the event target.
48
8/4/2019 Chapter5 New [EDocFind.com][1]
49/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
JavaScript event listener: function that is
called with Event instance when a certainevent occurs
An event listener is associated with a
target element by calling
addEventListener() on the element
49
8/4/2019 Chapter5 New [EDocFind.com][1]
50/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
50
8/4/2019 Chapter5 New [EDocFind.com][1]
51/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
Event
target
51
8/4/2019 Chapter5 New [EDocFind.com][1]
52/60
8/4/2019 Chapter5 New [EDocFind.com][1]
53/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
DOM event types:
All HTML intrinsic events except keypress,
keydown, keyup, and dblclick
Also has some others that are typically
targeted at the window object:
53
8/4/2019 Chapter5 New [EDocFind.com][1]
54/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
Event handler
Definition
of event
handler
54
8/4/2019 Chapter5 New [EDocFind.com][1]
55/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling
Event instance
55
8/4/2019 Chapter5 New [EDocFind.com][1]
56/60
8/4/2019 Chapter5 New [EDocFind.com][1]
57/60
8/4/2019 Chapter5 New [EDocFind.com][1]
58/60
8/4/2019 Chapter5 New [EDocFind.com][1]
59/60
Jackson, Web Technologies: A Computer Science Perspective, 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
DOM Event Handling:
Mouse Events
59
8/4/2019 Chapter5 New [EDocFind.com][1]
60/60