+ All Categories
Home > Documents > 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and...

2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and...

Date post: 29-Dec-2015
Category:
Upload: nathan-clarke
View: 215 times
Download: 0 times
Share this document with a friend
48
2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing 13.3 Collections all and children 13.4 Dynamic Styles 13.5 Dynamic Positioning 13.6 Using the frames Collection 13.7 navigator Object 13.8 Summary of the DHTML Object Model
Transcript
Page 1: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

1

Chapter 13 - Dynamic HTML: Object Model and Collections

Outline13.1 Introduction 13.2 Object Referencing 13.3 Collections all and children 13.4 Dynamic Styles 13.5 Dynamic Positioning 13.6 Using the frames Collection 13.7 navigator Object 13.8 Summary of the DHTML Object Model

Page 2: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline2

Reference.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 13.1: reference.html -->6 <!-- Object Model Introduction --> 7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Object Model</title>11 12 <script type = "text/javascript">13 <!--14 function start() 15 {16 alert( pText.innerText );17 pText.innerText = "Thanks for coming.";18 }19 // -->20 </script>21 22 </head>23 24 <body onload = "start()">25 <p id = "pText">Welcome to our Web page!</p>26 </body>27 </html>

The innerText property of the object refers to the text contained in that element

(Welcome to our Web page!).

Function start displays an alert box containing the value of pText.innerText

The onload event calls the JavaScript start function when document loading completes.

The innerText property of the object is dynamically

changed (Thanks for coming.).

Page 3: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline3

Program Output

The value of pText.innerText after the function start is invoked.

The value of pText.innerText when the page is first loaded.

An alert box displaying the value of pText.innerText.

Page 4: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline4

All.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig 13.2: all.html -->6 <!-- Using the all collection -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Object Model</title>11 12 <script type = "text/javascript">13 <!--14 var elements = "";15 16 function start() 17 {18 for ( var loop = 0; loop < document.all.length; ++loop ) 19 elements += "<br />" + document.all[ loop ].tagName;20 21 pText.innerHTML += elements;22 alert( elements );23 }24 // -->25 </script>26 </head>27 28 <body onload = "start()">29 <p id = "pText">Elements on this Web page:</p>30 </body>31 </html>

The for loop loops through the elements of the all collection and display each element’s name.

The length property of the all collection specifies the number of elements in the collection.

The name of each XHTML element (given in the tagName property) in the

collection is appended to elements.

The innerHTML property is similar to the innerText property but can also include XHTML formatting.

The all collection is a collection of all the XHTML elements in the page in the order they appear.

Page 5: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline5

Program Output

An alert dialog is displayed with all the names of the XHTML elements in the all collection.

The XHTML elements are rendered on the page. Note comments are represented as !.

Page 6: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline6

Children.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig 13.3: children.html -->6 <!-- The children collection -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Object Model</title>11 12 <script type = "text/javascript">13 <!--14 var elements = "<ul>";15 16 function child( object ) 17 { 18 var loop = 0;19 20 elements += "<li>" + object.tagName + "<ul>"; 21 22 for ( loop = 0; loop < object.children.length; loop++ )23 {24 if ( object.children[ loop ].children.length )25 child( object.children[ loop ] );26 else27 elements += "<li>" +28 object.children[ loop ].tagName + 29 "</li>";30 }31 32 elements += " </ul> ";33 }34 // -->

Function child uses recursion to view all elements on a page.

The script adds ul and li tags to display the elements on the page in a hierarchical manner.

If an element has children function child recurses on itself with the child to print

these elements out.

The children collection for a specific element contains that

element’s child elements.

Page 7: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline7

Children.html

Program Output

35 </script>36 </head>37 38 <body onload = "child( document.all[ 4 ] ); 39 myDisplay.outerHTML += elements;">40 41 <p>Welcome to our <strong>Web</strong> page!</p>42 43 <p id = "myDisplay">44 Elements on this Web page:45 </p>46 47 </body>48 </html>

Property outerHTML is similar to property innerHTML but it includes the enclosing XHTML tags (tags <p id = "myDisplay"> and </p> in

this case) as well as the content inside them.

A display of all the elements in a document.

Page 8: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline8

Dynamicstyle.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 13.4: dynamicstyle.html -->6 <!-- Dynamic Styles -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Object Model</title>11 12 <script type = "text/javascript">13 <!--14 function start() 15 {16 var inputColor = prompt(17 "Enter a color name for the " + 18 "background of this page", "" );19 document.body.style.backgroundColor = inputColor; 20 }21 // -->22 </script>23 </head>24 25 <body onload = "start()">26 <p>Welcome to our Web site!</p>27 </body>28 </html>

Function start prompts the user to enter a color and then sets the background to that color.

A prompt dialog box is displayed prompting the user to input a color. The color is stored in variable inputColor.

The input color is set as the background color.

Page 9: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline9

Program Output

Initial Output

Prompt dialog asking user to enter a background color.

Final output with background color selected by the user set.

Page 10: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline10

Dynamicstyle2.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 13.5: dynamicstyle2.html -->6 <!-- More Dynamic Styles -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Object Model</title>11 12 <style type = "text/css">13 14 .bigText { font-size: 3em;15 font-weight: bold }16 17 .smallText { font-size: .75em }18 19 </style>20 21 <script type = "text/javascript">22 <!--23 function start() 24 {25 var inputClass = prompt( 26 "Enter a className for the text " + 27 "(bigText or smallText)", "" );28 pText.className = inputClass;29 }30 // -->31 </script>32 </head>33

The user is prompted to enter one of the two defined styles to be applied to the text.

Two style classes for font are defined.

Page 11: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline11

Dynamicstyle2.html

Program Output

34 <body onload = "start()">35 <p id = "pText">Welcome to our Web site!</p>36 </body>37 </html>

Font after user decides to apply the bigText style to the text.

Dialog prompting user to enter a font style.

Page 12: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline12

Dynamicposition.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 13.6: dynamicposition.html -->6 <!-- Dynamic Positioning -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Dynamic Positioning</title>11 12 <script type = "text/javascript">13 <!--14 var speed = 5;15 var count = 10;16 var direction = 1;17 var firstLine = "Text growing";18 var fontStyle = [ "serif", "sans-serif", "monospace" ];19 var fontStylecount = 0;20 21 function start() 22 {23 window.setInterval( "run()", 100 );24 }25 26 function run() 27 {28 count += speed;29 30 if ( ( count % 200 ) == 0 ) {31 speed *= -1;32 direction = !direction;33

Function start is used to update the p element’s content.

Function setInternal takes in a function name and how often to run that function.

Function run will reverse between increasing and decreasing font size

and blue and red font color.

Page 13: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline13

Dynamicposition.html

34 pText.style.color = 35 ( speed < 0 ) ? "red" : "blue" ;36 firstLine = 37 ( speed < 0 ) ? "Text shrinking" : "Text growing";38 pText.style.fontFamily = 39 fontStyle[ ++fontStylecount % 3 ];40 }41 42 pText.style.fontSize = count / 3;43 pText.style.left = count;44 pText.innerHTML = firstLine + "<br /> Font size: " + 45 count + "px";46 }47 // -->48 </script>49 </head>50 51 <body onload = "start()">52 <p id = "pText" style = "position: absolute; left: 0; 53 font-family: serif; color: blue">54 Welcome!</p>55 </body>56 </html>

Font size and color will be selected based on the value of speed.

Page 14: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline14

Program Output

Once count reaches a multiple of 200, the size of the text font starts to shrink by one third.

Page 15: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline15

Index.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">4 5 <!-- Fig. 13.7: index.html -->6 <!-- Using the frames collection -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>Frames collection</title>11 </head>12 13 <frameset rows = "100, *">14 <frame src = "top.html" name = "upper" />15 <frame src = "" name = "lower" />16 </frameset>17 18 </html>

The browser window is broken into two horizontal frames.

The top frame (upper) displays file top.html.

The bottom frame (lower) is initially empty.

Page 16: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline16

Top.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 13.8: top.html -->6 <!-- Cross-frame scripting -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>The frames collection</title>11 12 <script type = "text/javascript">13 <!--14 function start()15 {16 var text = prompt( "What is your name?", "" );17 parent.frames( "lower" ).document.write( 18 "<h1>Hello, " + text + "</h1>" );19 }20 // -->21 </script>22 </head> 23 24 <body onload = "start()">25 <h1>Cross-frame scripting!</h1>26 </body>27 </html>

Function start takes in a user’s name and writes it in a frame in the browser.

The write function is used to write text to the frame in the browser.

The parent frame of the current frame is first referenced following that the lower frame is referenced.

Page 17: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline17

Program Output

Browser prior to user entering a name.

Dialog prompt for user to enter name.

Browser updated with user name and Hello displayed in

bottom frame.

Page 18: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline18

Navigator.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig 13.9: navigator.html -->6 <!-- Using the navigator object -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>The navigator Object</title>11 12 <script type = "text/javascript">13 <!--14 function start() 15 {16 if (navigator.appName=="Microsoft Internet Explorer")17 {18 if ( navigator.appVersion.substring( 1, 0 ) >= "4" )19 document.location = "newIEversion.html"; 20 else21 document.location = "oldIEversion.html";22 }23 else 24 document.location = "NSversion.html"; 25 }26 // -->27 </script>28 </head>29 30 <body onload = "start()">31 <p>Redirecting your browser to the appropriate page, 32 please wait...</p>33 </body>34 </html>

On load function start is called to determine what browser is being

used. Based on this a page specific to that browser is displayed.

The browser is Microsoft Internet Explorer.

The function appVersion is used to determine which

version of IE is used.The browser is Netscape.

Page 19: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline19

Program Output

The document specific for the Microsoft Internet Explorer

browser is displayed.

The document specific for the Netscape browser is displayed.

(Netscape Communicator browser window© 1999 Netscape Communications Corporation. Used with permission. Netscape Communications has not authorized, sponsored, endorsed, or approved this publication and is not responsible for its content.)

Page 20: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

20

13.8 Summary of the DHTML Object Model

applets

all

anchors

embeds

forms

filters

images

links

plugins

styleSheets

scripts

frames

plugins

collection

body

screen

document

history

navigator

location

event

document

document

object

window

Key

Fig. 13.10 DHTML Object Model.

Page 21: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

21

13.8 Summary of the DHTML Object Model

Object or collection Description

Objects

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 XHTML document rendered in a window. The document object provides access to every element in the XHTML document and allows dynamic modification of the XHTML document.

body This object provides access to the body element of an XHTML 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.

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.

Fig. 13.11 Objects in the Internet Explorer 5.5 object model.

Page 22: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

22

13.8 Summary of the DHTML Object Model

Object or collection Description

Collections

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 XHTML 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 XHTML document.

applets This collection contains all the applet elements in the XHTML document. Currently, the

most common applet elements are Java applets.

embeds This collection contains all the embed elements in the XHTML document.

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

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 XHTML document. The elements appear in the collection in the order they were defined in the XHTML 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 XHTML document.

scripts This collection contains all the script elements in the XHTML document.

styleSheets This collection contains styleSheet objects that represent each style element in the

XHTML document and each style sheet included in the XHTML document via link.

Fig. 13.11 Objects in the Internet Explorer 5.5 object model.

Page 23: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

The Event Model

The Event Model

23

Page 24: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline24

Onclick.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 5 <!-- Fig 14.1: onclick.html -->6 <!-- Demonstrating the onclick event -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - onclick</title>11 12 <!-- The for attribute declares the script for -->13 <!-- a certain element, and the event for a -->14 <!-- certain event. -->15 <script type = "text/javascript" for = "para" 16 event = "onclick">17 <!--18 alert( "Hi there" );19 // -->20 </script>21 </head>22 23 <body>24 25 <!-- The id attribute gives a unique identifier -->26 <p id = "para">Click on this text!</p> 27 28 <!-- You can specify event handlers inline -->29 <input type = "button" value = "Click Me!" 30 onclick = "alert( 'Hi again' )" />31 32 </body>33 </html>

The script element will display an alert dialog box if the onclick event occurs

for the element whose id is para.

JavaScript enables the user to respond to events.

The onclick event occurs when the user clicks the mouse.

The p element is assigned an id that can be used to reference it.

The input element creates a button that displays an alert when clicked.

Page 25: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline25

Program Output

Button created by the input element (lines 29-30).

Alert invoked by script (lines 15-20).

Alert invoked by the input element (lines 29-30).

Page 26: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline26

Onload.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig. 14.2: onload.html -->6 <!-- Demonstrating the onload event -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - onload</title>11 <script type = "text/javascript">12 <!--13 var seconds = 0;14 15 function startTimer() {16 // 1000 milliseconds = 1 second17 window.setInterval( "updateTime()", 1000 );18 }19 20 function updateTime() {21 seconds++;22 soFar.innerText = seconds; 23 }24 // -->25 </script>26 </head>27 28 <body onload = "startTimer()">29 30 <p>Seconds you have spent viewing this page so far:31 <a id = "soFar"><strong>0</strong></a></p>32 33 </body>34 </html>

Function startTimer will call function updateTime every 1000 milliseconds.

Method window.setInterval is used to invoke function updateTime every second.

Function updateTime sets the innerText property of the element with soFar as an

id to the number of seconds that have elapsed since loading.

The onload event executes when an element finishes loading.

Page 27: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline27

Program Output

The page will dynamically update the number of seconds that have elapsed since the page has loaded every second.

Page 28: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline28

Onerror.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 5 <!-- Fig 14.3: onerror.html -->6 <!-- Demonstrating the onerror event -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - onerror</title>11 <script type = "text/javascript">12 <!--13 // Specify that if an onerror event is triggered 14 // in the window function handleError should execute15 window.onerror = handleError;16 17 function doThis() {18 alrrt( "hi" ); // alert misspelled, creates an error19 }20 21 // The ONERROR event passes three values to the 22 // function: the name of the error, the url of 23 // the file, and the line number.24 function handleError( errType, errURL, errLineNum ) 25 {26 // Writes to the status bar at the 27 // bottom of the window.28 window.status = "Error: " + errType + " on line " + 29 errLineNum;30 31 // Returning a value of true cancels the 32 // browser’s reaction.33 return true;34 }35 // -->

The onerror event allows the developer to handle errors more elegantly.

If an onerror event in triggered the function

handleError will be invoked.

The call to display the alert dialog is purposely

written incorrectly to invoke the onerror event.

Function handleError will display the error type and the line that causes the error on

the status bar of the browser.Returning true indicates that the error has been handled successfully.

Page 29: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline29

Onerror.html

Program Output

36 </script>37 </head>38 39 <body>40 41 <input id = "mybutton" type = "button" value = "Click Me!"42 onclick = "doThis()" />43 44 </body>45 </html>

C ustom error output

The error created by trying to invoke function doThis is handled by the message in the status bar of the browser.

Page 30: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline30

Onmousemove.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 5 <!-- Fig. 14.4: onmousemove.html -->6 <!-- Demonstrating the onmousemove event -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - onmousemove event</title>11 <script type = "text/javascript">12 <!--13 function updateMouseCoordinates() 14 {15 coordinates.innerText = event.srcElement.tagName + 16 " (" + event.offsetX + ", " + event.offsetY + ")";17 }18 // -->19 </script>20 </head>21 22 <body style = "back-groundcolor: wheat"23 onmousemove = "updateMouseCoordinates()">24 25 <span id = "coordinates">(0, 0)</span><br />26 <img src = "deitel.gif" style = "position: absolute;27 top: 100; left: 100" alt = "Deitel" /> 28 29 </body>30 </html>

The innerText property of the coordinates element will be assigned a

string containing the name of the element, and the coordinates of the mouse position over the element.

The offsetX and offsetY properties of the event object give the location of the mouse cursor relative to the top-left corner of the object on which the event was triggered.

The onmousemove event is invoked every time the user moves the mouse.

Page 31: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline31

Program Output

Updated text (keeps changing as you move the mouse)

The mouse is over the body of the page as indicated by the text at the top right.

The mouse is over the image on the page as indicated by the text at the top right.

Page 32: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

32

14.6 Rollovers with onmouseover and onmouseout

Property of event Description

altkey This value is true if Alt key was pressed when event fired.

button Returns which mouse button was pressed by user (1: left-mouse button, 2: right-mouse button, 3: left and right buttons, 4: middle button, 5: left and middle buttons, 6: right and middle buttons, 7: all three buttons).

cancelBubble Set to false to prevent this event from bubbling (see Section 14.9, “Event Bubbling”).

clientX / clientY The coordinates of the mouse cursor inside the client area (i.e., the active area where the Web page is displayed, excluding scrollbars, navigation buttons, etc.).

ctrlKey This value is true if Ctrl key was pressed when event fired.

offsetX / offsetY The coordinates of the mouse cursor relative to the object that fired the event.

propertyName The name of the property that changed in this event.

recordset A reference to a data field’s recordset (see Chapter 16, “Data Binding”).

returnValue Set to false to cancel the default browser action.

screenX / screenY The coordinates of the mouse cursor on the screen coordinate system.

shiftKey This value is true if Shift key was pressed when event fired.

srcElement A reference to the object that fired the event.

type The name of the event that fired.

x / y The coordinates of the mouse cursor relative to this element’s parent element.

Fig. 14.5 Some event object properties.

Page 33: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline33

Onmouseoverout.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig 14.6: onmouseoverout.html -->6 <!-- Events onmouseover and onmouseout -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>11 DHTML Event Model - onmouseover and onmouseout12 </title>13 <script type = "text/javascript">14 <!--15 captionImage1 = new Image();16 captionImage1.src = "caption1.gif";17 captionImage2 = new Image();18 captionImage2.src = "caption2.gif";19 20 function mOver() 21 {22 if ( event.srcElement.id == "tableCaption" ) {23 event.srcElement.src = captionImage2.src;24 return;25 }26 27 // If the element which triggered onmouseover has 28 // an id, change its color to its id.29 if ( event.srcElement.id )30 event.srcElement.style.color = 31 event.srcElement.id;32 }33

The function mOver handles the onmouseover event for the image

by setting its src attribute to the src property of the appropriate image.

Page 34: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline34

Onmouseoverout.html

34 function mOut() 35 {36 if ( event.srcElement.id == "tableCaption" ) {37 event.srcElement.src = captionImage1.src;38 return;39 }40 41 // If it has an id, change the text inside to the 42 // text of the id.43 if ( event.srcElement.id )44 event.srcElement.innerText = event.srcElement.id;45 }46 47 document.onmouseover = mOver;48 document.onmouseout = mOut;49 // -->50 </script>51 </head>52 53 <body style = "background-color: wheat">54 55 <h1>Guess the Hex Code's Actual Color</h1>56 57 <p>Can you tell a color from its hexadecimal RGB code 58 value? Look at the hex code, guess the color. To see 59 what color it corresponds to, move the mouse over the 60 hex code. Moving the mouse out will display the color 61 name.</p>62 63 <table style = "width: 50%; border-style: groove; 64 text-align: center; font-family: monospace; 65 font-weight: bold">66

The onmouseover event occurs when the mouse cursor moves

over an element.

The function mOut handles the onmouseout event for the image. It works similarly to

the mOver function.

The onmouseout event occurs when the mouse cursor leaves the element.

This code tests if an id is specified, and if it is, the code changes the color of the

element to match the color name in the id.

Page 35: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline35

Onmouseoverout.html

67 <caption>68 <img src = "caption1.gif" id = "tableCaption" 69 alt = "Table Caption" />70 </caption>71 72 <tr>73 <td><a id = "Black">#000000</a></td>74 <td><a id = "Blue">#0000FF</a></td>75 <td><a id = "Magenta">#FF00FF</a></td> 76 <td><a id = "Gray">#808080</a></td>77 </tr>78 <tr>79 <td><a id = "Green">#008000</a></td> 80 <td><a id = "Lime">#00FF00</a></td> 81 <td><a id = "Maroon">#800000</a></td>82 <td><a id = "Navy">#000080</a></td> 83 </tr>84 <tr>85 <td><a id = "Olive">#808000</a></td> 86 <td><a id = "Purple">#800080</a></td>87 <td><a id = "Red">#FF0000</a></td>88 <td><a id = "Silver">#C0C0C0</a></td> 89 </tr>90 <tr>91 <td><a id = "Cyan">#00FFFF</a></td>92 <td><a id = "Teal">#008080</a></td>93 <td><a id = "Yellow">#FFFF00</a></td> 94 <td><a id = "White">#FFFFFF</a></td> 95 </tr>96 </table>97 98 </body>99 </html>

Page 36: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline36

Program Output

Onrollover the text will change to the color of the corresponding hex code.

Page 37: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline37

Program Output

Onmouseout the hex value will be replaced by the color it represents.

Page 38: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline38

Onfocusblur.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 5 <!-- Fig. 14.7: onfocusblur.html -->6 <!-- Demonstrating the onfocus and onblur events -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - onfocus and onblur</title>11 <script type = "text/javascript">12 <!--13 var helpArray = 14 [ "Enter your name in this input box.",15 "Enter your email address in this input box, " +16 "in the format user@domain.",17 "Check this box if you liked our site.",18 "In this box, enter any comments you would " +19 "like us to read.",20 "This button submits the form to the " +21 "server-side script",22 "This button clears the form",23 "This textarea provides context-sensitive " +24 "help. Click on any input field or use the TAB " +25 "key to get more information about the " + 26 "input field." ];27 28 function helpText( messageNum ) 29 {30 myForm.helpBox.value = helpArray[ messageNum ];31 }32 // -->33 </script>34 </head>35

The script changes the text inside the text box in the upper-right

corner based on the messageNum passed to helpText.

Page 39: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline39

Onfocusblur.html

36 <body>37 38 <form id = "myForm" action = "">39 Name: <input type = "text" name = "name" 40 onfocus = "helpText(0)" onblur = "helpText(6)" /><br />41 Email: <input type = "text" name = "email" 42 onfocus = "helpText(1)" onblur = "helpText(6)" /><br />43 Click here if you like this site 44 <input type = "checkbox" name = "like" onfocus = 45 "helpText(2)" onblur = "helpText(6)" /><br /><hr />46 47 Any comments?<br />48 <textarea name = "comments" rows = "5" cols = "45" 49 onfocus = "helpText(3)" onblur = "helpText(6)">50 </textarea><br />51 <input type = "submit" value = "Submit" onfocus = 52 "helpText(4)" onblur = "helpText(6)" />53 <input type = "reset" value = "Reset" onfocus = 54 "helpText(5)" onblur = "helpText(6)" />55 56 <textarea name = "helpBox" style = "position: absolute; 57 right: 0; top: 0" rows = "4" cols = "45">58 This textarea provides context-sensitive help. Click on 59 any input field or use the Tab key to get more information 60 about the input field.</textarea>61 </form>62 63 </body>64 </html>

The onfocus event fires when an element gains focus.

The onblur event fires when an element loses focus, which occurs when another control gains the focus.

Page 40: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline40

Program Output

The other elements are currently onblur.

The focus in this form is currently on the email text box as could be seen by the location of the mouse cursor.

Page 41: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline41

Onsubmitreset.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 5 <!-- Fig 14.8: onsubmitreset.html -->6 <!-- Demonstrating the onsubmit and onreset events -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>11 DHTML Event Model - onsubmit and onreset events12 </title>13 <script type = "text/javascript">14 <!--15 var helpArray = 16 [ "Enter your name in this input box.",17 "Enter your email address in this input box, " +18 "in the format user@domain.",19 "Check this box if you liked our site.",20 "In this box, enter any comments you would " +21 "like us to read.",22 "This button submits the form to the " +23 "server-side script",24 "This button clears the form",25 "This textarea provides context-sensitive " +26 "help. Click on any input field or use the Tab " +27 "key to get more information about " + 28 "the input field." ];29 30 function helpText( messageNum ) 31 {32 myForm.helpBox.value = helpArray[ messageNum ];33 }34

Page 42: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline42

Onsubmitreset.html

35 function formSubmit() {36 window.event.returnValue = false;37 38 if ( confirm ( "Are you sure you want to submit?" ) )39 window.event.returnValue = true;40 }41 42 function formReset() {43 window.event.returnValue = false;44 45 if ( confirm( "Are you sure you want to reset?" ) )46 window.event.returnValue = true;47 }48 // -->49 </script>50 </head>51 52 <body>53 54 <form id = "myForm" onsubmit = "formSubmit()" 55 onreset = "formReset()" action = "">56 Name: <input type = "text" name = "name" 57 onfocus = "helpText(0)" onblur = "helpText(6)" /><br />58 Email: <input type = "text" name = "email" 59 onfocus = "helpText(1)" onblur = "helpText(6)" /><br />60 Click here if you like this site 61 <input type = "checkbox" name = "like" onfocus = 62 "helpText(2)" onblur = "helpText(6)" /><hr />63 64 Any comments?<br />65 <textarea name = "comments" rows = "5" cols = "45" 66 onfocus = "helpText(3)" onblur = "helpText(6)">67 </textarea><br />68 <input type = "submit" value = "Submit" onfocus = 69 "helpText(4)" onblur = "helpText(6)" />

The returnValue property is set to false and cancels

the default action of the event on the element.

A dialog to ask the user to confirm the action is displayed, if the user confirms then the action is executed on the form.

Page 43: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline43

Onsubmitreset.html

Program Output

70 <input type = "reset" value = "Reset" onfocus = 71 "helpText(5)" onblur = "helpText(6)" />72 73 <textarea name = "helpBox" style = "position: absolute; 74 right:0; top: 0" rows = "4" cols = "45">75 This textarea provides context-sensitive help. Click on 76 any input field or use the Tab key to get more 77 information about the input field.</textarea>78 </form>79 80 </body>81 </html>

When the user clicks on the Submit button, a dialog pops up asking the

user to confirm the submission.

Page 44: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline44

Bubbling.html

1 <?xml version = "1.0"?>2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4 5 <!-- Fig 14.9: bubbling.html -->6 <!-- Disabling event bubbling -->7 8 <html xmlns = "http://www.w3.org/1999/xhtml">9 <head>10 <title>DHTML Event Model - Event Bubbling</title>11 12 <script type = "text/javascript">13 <!--14 function documentClick() 15 {16 alert( "You clicked in the document" ); 17 }18 19 function paragraphClick( value ) 20 {21 alert( "You clicked the text" );22 23 if ( value ) 24 event.cancelBubble = true;25 }26 27 document.onclick = documentClick;28 // -->29 </script>30 </head>31

By setting the cancelBubble method to true, disables event bubbling.

Page 45: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc.All rights reserved.

Outline45

Bubbling.html

Program Output

32 <body>33 34 <p onclick = "paragraphClick( false )">Click here!</p> 35 <p onclick = "paragraphClick( true )">Click here, too!</p>36 </body>37 </html>

The event has bubbled up to the document level

The event has been cance led

Clicking on the first p element triggers line 27 because the

onclick event has bubbled up to the document level.

Clicking on the second p element passes a value of true to function paragraphClick, which will disable the event

bubbling for this event.

Page 46: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

46

14.10 More DHTML Events

Event Description

Clipboard events

onbeforecut Fires before a selection is cut to the clipboard.

onbeforecopy Fires before a selection is copied to the clipboard.

onbeforepaste Fires before a selection is pasted from the clipboard.

oncopy Fires when a selection is copied to the clipboard.

oncut Fires when a selection is cut to the clipboard.

onabort Fires if image transfer has been interrupted by user.

onpaste Fires when a selection is pasted from the clipboard.

Data binding events

onafterupdate Fires immediately after a databound object has been updated.

onbeforeupdate Fires before a data source is updated.

oncellchange Fires when a data source has changed.

ondataavailable Fires when new data from a data source become available.

ondatasetchanged Fires when content at a data source has changed.

ondatasetcomplete Fires when transfer of data from the data source has completed.

onerrorupdate Fires if an error occurs while updating a data field.

onrowenter Fires when a new row of data from the data source is available.

onrowexit Fires when a row of data from the data source has just finished.

onrowsdelete Fires when a row of data from the data source is deleted.

onrowsinserted Fires when a row of data from the data source is inserted.

Fig. 14.10 Dynamic HTML events.

Page 47: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

47

14.10 More DHTML Events

Event Description

Keyboard Events

onhelp Fires when the user initiates help (i.e., by pressing the F1 key).

onkeydown Fires when the user pushes down a key.

onkeypress Fires when the user presses a key.

onkeyup Fires when the user ends a key press.

marquee events onbounce Fires when a scrolling marquee bounces back in the other

direction.

onfinish Fires when a marquee finishes its scrolling.

onstart Fires when a marquee begins a new loop.

Mouse events

oncontextmenu Fires when the context menu is shown (right-click).

ondblclick Fires when the mouse is double-clicked.

ondrag Fires during a mouse drag.

ondragend Fires when a mouse drag ends.

ondragenter Fires when something is dragged onto an area.

ondragleave Fires when something is dragged out of an area.

ondragover Fires when a drag is held over an area.

ondragstart Fires when a mouse drag begins.

ondrop Fires when a mouse button is released over a valid target during a drag.

onmousedown Fires when a mouse button is pressed down.

onmouseup Fires when a mouse button is released.

Fig. 14.10 Dynamic HTML events.

Page 48: 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.

2001 Prentice Hall, Inc. All rights reserved.

48

14.10 More DHTML Events

Event Description

Miscellaneous Events

onafterprint Fires immediately after the document prints.

onbeforeeditfocus Fires before an element gains focus for editing.

onbeforeprint Fires before a document is printed.

onbeforeunload Fires before a document is unloaded (i.e., the window was closed or a link was clicked).

onchange Fires when a new choice is made in a select element, or when a text input is changed and the element loses focus.

onfilterchange Fires when a filter changes properties or finishes a transition (see Chapter 15, Filters and Transitions).

onlosecapture Fires when the releaseCapture method is invoked.

onpropertychange Fires when the property of an object is changed.

onreadystatechange Fires when the readyState property of an element changes.

onreset Fires when a form resets (i.e., the user clicks an <input type = "reset">).

onresize Fires when the size of an object changes (i.e., the user resizes a window or frame).

onscroll Fires when a window or frame is scrolled.

onselect Fires when a text selection begins (applies to input or textarea).

onselectstart Fires when the object is selected.

onstop Fires when the user stops loading the object.

onunload Fires when a page is about to unload.

Fig. 14.10 Dynamic HTML events.


Recommended