33
1 2001 Deitel & Associates, Inc. All rights reserved. Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1 Introduction 20.2 Object Referencing 20.3 Collections all and children 20.4 Dynamic Styles 20.5 Dynamic Positioning 20.6 Using the frames Collection 20.7 navigator Object 20.8 Summary of the DHTML Object Model

2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

Embed Size (px)

Citation preview

Page 1: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

1

2001 Deitel & Associates, Inc. All rights reserved.

Chapter 20 – Dynamic HTML: Object Model and Collections

Outline20.1 Introduction 20.2 Object Referencing 20.3 Collections all and children 20.4 Dynamic Styles 20.5 Dynamic Positioning 20.6 Using the frames Collection 20.7 navigator Object 20.8 Summary of the DHTML Object Model

Page 2: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2

2001 Deitel & Associates, Inc. All rights reserved.

20.1 Introduction

• Dynamic HTML object model– Great control over presentation of pages

• Access to all elements on the page

– Whole web page (elements, forms, frames, tables, etc.) represented in an object hierarchy

• HTML elements treated as objects– Attributes of these elements treated as properties of

those objects• Objects identified with an ID attribute can be scripted with

languages like JavaScript, JScript and VBScript

Page 3: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

3

2001 Deitel & Associates, Inc. All rights reserved.

20.2 Object Referencing

• Simplest way to reference an element is by its ID attribute

• Changing the text displayed on screen – Example of a Dynamic HTML ability called dynamic

content

Page 4: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1.1innerText property

Object pText refers to the P element whose ID is set to pText (line 22)

innertext property refers to text contained in element

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2<HTML>

3

4<!-- Fig. 20.1: example1.html -->

5<!-- Object Model Introduction -->

6

7<HEAD>

8<TITLE>Object Model</TITLE>

9

10<SCRIPT LANGUAGE = "JavaScript">

11 function start()

12 {

13 alert( pText.innerText );

14 pText.innerText = "Thanks for coming.";

15 }

16</SCRIPT>

17

18</HEAD>

19

20<BODY ONLOAD = "start()">

21

22<P ID = "pText">Welcome to our Web page!</P>

23

24</BODY>

25</HTML>

Page 5: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

5

2001 Deitel & Associates, Inc. All rights reserved.

Object referencing with the Dynamic HTML Object Model

Page 6: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

6

2001 Deitel & Associates, Inc. All rights reserved.

20.3 Collections all and children

• Collections are basically arrays of related objects on a page

• all – Collection of all the HTML elements in a document in

the order in which they appear• length property

– Specifies the number of elements in the collection• tagName property of an element

– Determines the name of the element

• Every element has its own all collection, consisting of all the elements contained within that element

Page 7: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1. Document’s all collection

1.1 Loop through the collection and list the element names

2. <!DOCTYPE> and <!--> tags both have ! as their tagName

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">2<HTML>34<!-- Fig 20.2: all.html -->5<!-- Using the all collection -->67<HEAD>8<TITLE>Object Model</TITLE>910<SCRIPT LANGUAGE = "JavaScript">11 var elements = "";1213 function start() 14 {15 for ( var loop = 0; loop < document.all.length; ++loop ) 16 elements += "<BR>" + document.all[ loop ].tagName;1718 pText.innerHTML += elements;19 }20</SCRIPT>21</HEAD>2223<BODY ONLOAD = "start()">2425<P ID = "pText">Elements on this Web page:</P>2627</BODY>28</HTML>

Page 8: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

8

2001 Deitel & Associates, Inc. All rights reserved.

Looping through the all collection

Page 9: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

9

2001 Deitel & Associates, Inc. All rights reserved.

20.3 Collections all and children

• The children collection– Contains only those elements that are direct child

elements of that element

– An HTML element has only two children: the HEAD element and the BODY element

Page 10: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1. Document’s children collection

1.1 Loop recursively through the children collections

1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2 <HTML>

3

4 <!-- Fig 20.3: children.html -->

5 <!-- The children collection -->

6

7 <HEAD>

8 <TITLE>Object Model</TITLE>

9

10 <SCRIPT LANGUAGE = "JavaScript">

11 var elements = "<UL>";

12

13 function child( object )

14 {

15 var loop = 0;

16

17 elements += "<LI>" + object.tagName + "<UL>";

18

19 for ( loop = 0; loop < object.children.length; loop++ ) {

20

21 if ( object.children[loop].children.length )

22 child( object.children[ loop ] );

23 else

24 elements += "<LI>" + object.children[ loop ].tagName

25 + "</LI>";

26 }

27

28 elements += " </UL> ";

29 }

30 </SCRIPT>

Page 11: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

2. outerHTML property includes enclosing HTML tags as well as the content inside

31</HEAD>

32

33<BODY ONLOAD = "child( document.all[ 1 ] );

34 myDisplay.outerHTML += elements;">

35

36<P>Welcome to our <STRONG>Web</STRONG> page!</P>

37

38<P ID = "myDisplay">

39Elements on this Web page:

40</P>

41

42</BODY>

43</HTML>

Page 12: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

12

2001 Deitel & Associates, Inc. All rights reserved.

Navigating the object hierarchy using collection children

Page 13: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

13

2001 Deitel & Associates, Inc. All rights reserved.

20.4 Dynamic Styles

• Refer to the background color of a page as document.body.style.backgroundColor

• Dynamic HTML object model allows you to change the CLASS attribute of an element

Page 14: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1.1 Function start prompt’s the user to enter a color name, then sets the background color to that value

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2<HTML>

3

4<!-- Fig. 20.4: dynamicstyle.html -->

5<!-- Dynamic Styles -->

6

7<HEAD>

8<TITLE>Object Model</TITLE>

9

10<SCRIPT LANGUAGE = "JavaScript">

11 function start()

12 {

13 var inputColor = prompt( "Enter a color name for the " +

14 "background of this page", "" );

15 document.body.style.backgroundColor = inputColor;

16 }

17</SCRIPT>

18</HEAD>

19

20<BODY ONLOAD = "start()">

21

22<P>Welcome to our Web site!</P>

23

24</BODY>

25</HTML>

Page 15: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

15

2001 Deitel & Associates, Inc. All rights reserved.

Dynamic styles

Page 16: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1.1 Define style classes

1.2 Function start prompt’s the user for a class name and dynamically changes the font to reflect the user’s choice

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">2<HTML>34<!-- Fig. 20.5: dynamicstyle2.html -->5<!-- More Dynamic Styles -->67<HEAD>8<TITLE>Object Model</TITLE>910<STYLE>1112 .bigText { font-size: 3em;13 font-weight: bold }1415 .smallText { font-size: .75em }1617</STYLE>1819<SCRIPT LANGUAGE = "JavaScript">20 function start() 21 {22 var inputClass = prompt( "Enter a className for the text "23 + "(bigText or smallText)", "" );24 pText.className = inputClass;25 }26</SCRIPT>27</HEAD>2829<BODY ONLOAD = "start()">3031<P ID = "pText">Welcome to our Web site!</P>3233</BODY>34</HTML>

Page 17: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

17

2001 Deitel & Associates, Inc. All rights reserved.

Dynamic styles in action

Page 18: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

18

2001 Deitel & Associates, Inc. All rights reserved.

20.5 Dynamic Positioning

• Dynamic positioning– An element can be positioned with scripting

• setInterval function takes two parameters:

– A function name

– How often to run that function– clearInterval function stops the timer

• setTimeout function similar, but only calls the function once– clearTimeout function stops the timer

Page 19: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1.1setInterval function

1.2 color, fontFamily and fontSize attributes

1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

2 <HTML>

3

4 <!-- Fig. 20.6: dynamicposition.html -->

5 <!-- Dynamic Positioning -->

6

7 <HEAD>

8 <TITLE>Dynamic Positioning</TITLE>

9

10 <SCRIPT LANGUAGE = "JavaScript">

11 var speed = 5;

12 var count = 10;

13 var direction = 1;

14 var firstLine = "Text growing";

15 var fontStyle = [ "serif", "sans-serif", "monospace" ];

16 var fontStylecount = 0;

17

18 function start()

19 {

20 window.setInterval( "run()", 100 );

21 }

22

23 function run()

24 {

25 count += speed;

26

27 if ( ( count % 200 ) == 0 ) {

28 speed *= -1;

29 direction = !direction;

30

Page 20: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline31 pText.style.color =

32 ( speed < 0 ) ? "red" : "blue" ;

33 firstLine =

34 ( speed < 0 ) ? "Text shrinking" : "Text growing";

35 pText.style.fontFamily =

36 fontStyle[ ++fontStylecount % 3 ];

37 }

38

39 pText.style.fontSize = count / 3;

40 pText.style.left = count;

41 pText.innerHTML = firstLine + "<BR> Font size: " +

42 count + "px";

43 }

44</SCRIPT>

45</HEAD>

46

47<BODY ONLOAD = "start()">

48

49<P ID = "pText" STYLE = "position: absolute; left: 0;

50 font-family: serif; color: blue">

51Welcome!</P>

52

53</BODY>

54</HTML>

Page 21: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

21

2001 Deitel & Associates, Inc. All rights reserved.

Dynamic positioning

Page 22: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

22

2001 Deitel & Associates, Inc. All rights reserved.

20.6 Using the frames Collection

• frames collection– To reference a frame, use frames(“name”) where

name is the ID or NAME of the desired frame• Ex. frames(“lower”) refers to the element in the frames

collection with an ID or NAME of lower

Page 23: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1. FRAMESET file for cross-frame scripting

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">

2<HTML>

3

4<!-- Fig 20.7: index.html -->

5<!-- Using the frames collection -->

6

7<HEAD>

8 <TITLE>Frames collection</TITLE>

9</HEAD>

10

11<FRAMESET ROWS = "100, *">

12 <FRAME SRC = "top.html" NAME = "upper">

13 <FRAME SRC = "" NAME = "lower">

14</FRAMESET>

15

16</HTML>

Page 24: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

2. Access other frames using frames collection

17<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

18<HTML>

19

20<!-- Fig 20.8: top.html -->

21<!-- Cross-frame scripting -->

22

23<HEAD>

24<TITLE>The frames collection</TITLE>

25

26<SCRIPT LANGUAGE = "JavaScript">

27 function start()

28 {

29 var text = prompt( "What is your name?", "" );

30 parent.frames( "lower" ).document.write( "<H1>Hello, " +

31 text + "</H1>" );

32 }

33</SCRIPT>

34</HEAD>

35

36<BODY ONLOAD = "start()">

37

38<H1>Cross-frame scripting!</H1>

39

40

41</BODY>

42</HTML>

Page 25: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

25

2001 Deitel & Associates, Inc. All rights reserved.

Accessing other frames

Page 26: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

26

2001 Deitel & Associates, Inc. All rights reserved.

20.7 Navigator Object

• navigator object– Supported by Netscape Navigator and Internet Explorer

– navigator object contains info about the Web browser viewing the page

– navigator.appName contains the name of the application

• “Microsoft Internet Explorer”

• “Netscape”

– Value of navigator.appVersion not a simple integer

• Contains other info, such as OS

• When using a browser-specific technology – Make provisions for other browsers

Page 27: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

2001 Deitel & Associates, Inc. All rights reserved.

Outline

1. Using the navigator object to redirect to appropriate pages

1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">2<HTML>34<!-- Fig 20.9: navigator.html -->5<!-- Using the navigator object -->67<HEAD>8<TITLE>The navigator Object</TITLE>910<SCRIPT LANGUAGE = "JavaScript">11 function start() 12 {13 if ( navigator.appName == "Microsoft Internet Explorer" ) {

1415 if ( navigator.appVersion.substring( 1, 0 ) >= "4" )16 document.location = "newIEversion.html"; 17 else18 document.location = "oldIEversion.html";19 }20 else 21 document.location = "NSversion.html"; 22 }23</SCRIPT>24</HEAD>2526<BODY ONLOAD = "start()">2728<P>Redirecting your browser to the appropriate page, 29please wait...</P>3031</BODY>32</HTML>

Page 28: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

28

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

• DHTML object model – Allows script programmer to access every element in

an HTML document

– Every element a separate object

Page 29: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

29

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

window

document

history

navigator

applets

all

anchors

body

embeds

forms

filters

images

links

plugins

styleSheets

scripts

location

screen

event

document

document

plugins

object

collection

Key

frames

Page 30: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

30

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

• Objects in the Internet Explorer 5 object model

Object Description

window This object represents the browser window and provides access to the document object contained in the window. If the window contains frames, a separate window object is created automatically for each frame, to provide access to the document rendered in that frame. Frames are considered to be subwindows in the browser.

document This object represents the HTML document rendered in a window. The document object provides access to every element in the HTML document and allows dynamic modification of the HTML document.

body This object provides access to the BODY element of an HTML document.

history This object keeps track of the sites visited by the browser user. The object provides a script programmer with the ability to move forward and backward through the visited sites, but for security reasons does not allow the actual site URLs to be manipulated.

navigator This object contains information about the Web browser, such as the name of the browser, the version of the browser, the operating system on which the browser is running and other information that can help a script writer customize the user’s browsing experience.

Page 31: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

31

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

• Objects in the Internet Explorer 5 object model– Continued from previous slide

Object Description

location This object contains the URL of the rendered document. When this object is set to a new URL, the browser immediately switches (navigates) to the new location.

event This object can be used in an event handler to obtain information about the event that occurred (e.g., the mouse coordinates during a mouse event).

screen The object contains information about the computer screen for the computer on which the browser is running. Information such as the width and height of the screen in pixels can be used to determine the size at which elements should be rendered in a Web page.

Page 32: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

32

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

• Collections in the Internet Explorer 5 object modelCollection Description all Many objects have an all collection that provides access to

every element contained in the object. For example, the body object’s all collection provides access to every element in the BODY element of an HTML document.

anchors This collection contains all anchor elements (A) that have a NAME or ID attribute. The elements appear in the collection in the order they were defined in the HTML document.

applets This collection contains all the APPLET elements in the HTML document. Currently, the most common APPLET elements are Java applets.

embeds This collection contains all the EMBED elements in the HTML document.

forms This collection contains all the FORM elements in the HTML document. The elements appear in the collection in the order they were defined in the HTML document.

Page 33: 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing

33

2001 Deitel & Associates, Inc. All rights reserved.

20.8 Summary of the DHTML Object Model

• Collections in the Internet Explorer 5 object Model– Continued from the previous slide

Collection Description

frames This collection contains window objects that represent each frame in the browser window. Each frame is treated as its own subwindow.

images This collection contains all the IMG elements in the HTML document. The elements appear in the collection in the order they were defined in the HTML document.

links This collection contains all the anchor elements (A) with an HREF property. This collection also contains all the AREA elements that represent links in an image map.

plugins Like the embeds collection, this collection contains all the EMBED elements in the HTML document.

scripts This collection contains all the SCRIPT elements in the HTML document.

styleSheets This collection contains styleSheet objects that represent each STYLE element in the HTML document and each style sheet included in the HTML document via LINK..