Chapter 15 Dynamic HTML Object Model and Collections

Chapter 15 – Dynamic HTML: Object Model and Collections Outline 15. 1 Introduction 15. 2 Object Referencing 15. 3 Collections all and children 15. 4 Dynamic Styles 15. 5 Dynamic Positioning 15. 6 Using the frames Collection 15. 7 navigator Object 15. 8 Summary of the DHTML Object Model 2000 Deitel & Associates, Inc. All rights reserved.

15. 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 Java. Script, JScript and VBScript 2000 Deitel & Associates, Inc. All rights reserved.

15. 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 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 2<HTML> 3 4<!-- Fig. 15. 1: example 1. html --> 5<!-- Object Model Introduction --> 6 7<HEAD> 8<TITLE>Object Model</TITLE> 9 10<SCRIPT LANGUAGE = "Java. Script"> 11 function start() 12 { 13 alert( p. Text. inner. Text ); 14 p. Text. inner. Text = "Thanks for coming. "; 15 } 16</SCRIPT> 17 18</HEAD> 19 20<BODY ONLOAD = "start()"> 21 22<P ID = "p. Text">Welcome to our Web page! </P> 23 24</BODY> 25</HTML> 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. 1 inner. Text property Object p. Text refers to the P element whose ID is set to p. Text (line 22) innertext property refers to text contained in element

Object referencing with the Dynamic HTML Object Model 2000 Deitel & Associates, Inc. All rights reserved.

15. 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 • tag. Name 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 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 2<HTML> 3 4<!-- Fig 15. 2: all. html --> 5<!-- Using the all collection --> 6 7<HEAD> 8<TITLE>Object Model</TITLE> 9 10<SCRIPT LANGUAGE = "Java. Script"> 11 var elements = ""; 12 13 function start() 14 { 15 for ( var loop = 0; loop < document. all. length; ++loop ) 16 elements += "<BR>" + document. all[ loop ]. tag. Name; 17 18 p. Text. inner. HTML += elements; 19 } 20</SCRIPT> 21</HEAD> 22 23<BODY ONLOAD = "start()"> 24 25<P ID = "p. Text">Elements on this Web page: </P> 26 27</BODY> 28</HTML> 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Document’s all collection 2. 1. 1 Loop through the collection and list the element names 3. 2. <!DOCTYPE> and <!--> tags both have ! as their tag. Name

Looping through the all collection 2000 Deitel & Associates, Inc. All rights reserved.

15. 3 Collections all and children (II) • 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 2000 Deitel & Associates, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > <HTML> <!-- Fig 15. 3: children. html --> <!-- The children collection --> <HEAD> <TITLE>Object Model</TITLE> <SCRIPT LANGUAGE = "Java. Script"> var elements = "<UL>"; function child( object ) { var loop = 0; elements += "<LI>" + object. tag. Name + "<UL>"; for ( loop = 0; loop < object. children. length; loop++ ) { if ( object. children[loop]. children. length ) child( object. children[ loop ] ); else elements += "<LI>" + object. children[ loop ]. tag. Name + "</LI>"; } elements += " </UL> "; } </SCRIPT> 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Document’s children collection 2. 1. 1 Loop recursively through the children collections
![31</HEAD> Outline 32 33<BODY ONLOAD = "child( document. all[ 1 ] ); 34 my. 31</HEAD> Outline 32 33<BODY ONLOAD = "child( document. all[ 1 ] ); 34 my.](http://slidetodoc.com/presentation_image_h2/9cd337328688e07325a5bf657b6b51aa/image-11.jpg)
31</HEAD> Outline 32 33<BODY ONLOAD = "child( document. all[ 1 ] ); 34 my. Display. outer. HTML += elements; " > 35 36<P>Welcome to our <STRONG>Web</STRONG> page!</P> 37 38<P ID = "my. Display"> 39 Elements on this Web page: 40</P> 41 42</BODY> 43</HTML> 2000 Deitel & Associates, Inc. All rights reserved. 2. outer. HTML property includes enclosing HTML tags as well as the content inside

Navigating the object hierarchy using collection children 2000 Deitel & Associates, Inc. All rights reserved.

15. 4 Dynamic Styles • Refer to the background color of a page as document. body. style. background. Color • Dynamic HTML object model allows you to change the CLASS attribute of an element 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 2<HTML> Outline 3 4<!-- Fig. 15. 4: dynamicstyle. html --> 5<!-- Dynamic Styles --> 6 7<HEAD> 8<TITLE>Object Model</TITLE> 9 10<SCRIPT LANGUAGE = "Java. Script"> 11 function start() 12 { 13 var input. Color = prompt( "Enter a color name for the " + 14 "background of this page", "" ); 15 16 document. body. style. background. Color = input. Color; } 17</SCRIPT> 18</HEAD> 19 20<BODY ONLOAD = "start()"> 21 22<P>Welcome to our Web site! </P> 23 24</BODY> 25</HTML> 2000 Deitel & Associates, Inc. All rights reserved. 1. 1 Function start prompt’s the user to enter a color name, then sets the background color to that value

Dynamic styles 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 2<HTML> 3 4<!-- Fig. 15. 5: dynamicstyle 2. html --> 5<!-- More Dynamic Styles --> 6 7<HEAD> 8<TITLE>Object Model</TITLE> 9 10<STYLE> 11 12. big. Text { font-size: 3 em; 13 font-weight: bold } 14 15. small. Text { font-size: . 75 em } 16 17</STYLE> 18 19<SCRIPT LANGUAGE = "Java. Script"> 20 function start() 21 { 22 var input. Class = prompt( "Enter a class. Name for the text " 23 + "(big. Text or small. Text)", "" ); 24 p. Text. class. Name = input. Class; 25 } 26</SCRIPT> 27</HEAD> 28 29<BODY ONLOAD = "start()"> 30 31<P ID = "p. Text">Welcome to our Web site! </P> 32 33</BODY> 2000 Deitel & Associates, Inc. All rights reserved. 34</HTML> 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

Dynamic styles in action 2000 Deitel & Associates, Inc. All rights reserved.

15. 5 Dynamic Positioning • Dynamic positioning – An element can be positioned with scripting • set. Interval function takes two parameters: – A function name – How often to run that function – clear. Interval function stops the timer • set. Timeout function similar, but only calls the function once – clear. Timeout function stops the timer 2000 Deitel & Associates, Inc. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > <HTML> <!-- Fig. 15. 6: dynamicposition. html --> <!-- Dynamic Positioning --> <HEAD> <TITLE>Dynamic Positioning</TITLE> <SCRIPT LANGUAGE = "Java. Script"> var speed = 5; var count = 10; var direction = 1; var first. Line = "Text growing"; var font. Style = [ "serif", "sans-serif", "monospace" ]; var font. Stylecount = 0; function start() { window. set. Interval( "run()", 100 ); } function run() { count += speed; if ( ( count % 200 ) == 0 ) { speed *= -1; direction = !direction; 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. 1 set. Interval function 1. 2 color, font. Family and font. Size attributes

31 p. Text. style. color = 32 ( speed < 0 ) ? "red" : "blue" ; 33 first. Line = 34 ( speed < 0 ) ? "Text shrinking" : "Text growing"; 35 p. Text. style. font. Family = 36 font. Style[ ++font. Stylecount % 3 ]; 37 } 38 39 p. Text. style. font. Size = count / 3; 40 p. Text. style. left = count; 41 p. Text. inner. HTML = first. Line + "<BR> Font size: " + 42 count + "px"; 43 } 44</SCRIPT> 45</HEAD> 46 47<BODY ONLOAD = "start()"> 48 49<P ID = "p. Text" STYLE = "position: absolute; left: 0; 50 font-family: serif; color: blue" > 51 Welcome!</P> 52 53</BODY> 54</HTML> 2000 Deitel & Associates, Inc. All rights reserved. Outline

Dynamic positioning 2000 Deitel & Associates, Inc. All rights reserved.

15. 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 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Frameset//EN" > 2<HTML> 3 4<!-- Fig 15. 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> 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. FRAMESET file for cross-frame scripting

17<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 18<HTML> 19 20<!-- Fig 15. 8: top. html --> 21<!-- Cross-frame scripting --> 22 23<HEAD> 24<TITLE>The frames collection</TITLE> 25 26<SCRIPT LANGUAGE = "Java. Script"> 27 function start() 28 { 29 var text = prompt( "What is your name? ", "" ); 30 parent. frames( "lower" ). document. write( "<H 1>Hello, " + 31 text + "</H 1>" ); 32 } 33</SCRIPT> 34</HEAD> 35 36<BODY ONLOAD = "start()"> 37 38<H 1>Cross-frame scripting!</H 1> 39 40 41</BODY> 42</HTML> 2000 Deitel & Associates, Inc. All rights reserved. Outline 2. Access other frames using frames collection

Accessing other frames 2000 Deitel & Associates, Inc. All rights reserved.

15. 7 Navigator Object • navigator object – Supported by Netscape Navigator and Internet Explorer – navigator object contains info about the Web browser viewing the page – navigator. app. Name contains the name of the application • “Microsoft Internet Explorer” • “Netscape” – Value of navigator. app. Version not a simple integer • Contains other info, such as OS • When using a browser-specific technology – Make provisions for other browsers 2000 Deitel & Associates, Inc. All rights reserved.

1<!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 0 Transitional//EN" > 2<HTML> 3 4<!-- Fig 15. 9: navigator. html --> 5<!-- Using the navigator object --> 6 7<HEAD> 8<TITLE>The navigator Object</TITLE> 9 10<SCRIPT LANGUAGE = "Java. Script"> 11 function start() 12 { 13 if ( navigator. app. Name == "Microsoft Internet Explorer" ) { 14 15 if ( navigator. app. Version. substring( 1, 0 ) >= "4" ) 16 document. location = "new. IEversion. html"; 17 else 18 document. location = "old. IEversion. html"; 19 } 20 else 21 document. location = "NSversion. html"; 22 } 23</SCRIPT> 24</HEAD> 25 26<BODY ONLOAD = "start()"> 27 28<P>Redirecting your browser to the appropriate page, 29 please wait. . . </P> 30 31</BODY> 2000 Deitel & Associates, Inc. All rights reserved. 32</HTML> Outline 1. Using the navigator object to redirect to appropriate pages

15. 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 2000 Deitel & Associates, Inc. All rights reserved.

The DHTML Object Model window all document frames document anchors history document applets body navigator plugins location embeds event filters screen forms images links Key object plugins collection scripts style. Sheets 2000 Deitel & Associates, Inc. All rights reserved.
- Slides: 29