15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

default color (“red”) or any other color that is passed to the component. For details<br />

on the syntax of the tags, see Chapter 48 of the <strong>JavaScript</strong> <strong>Bible</strong>. The code<br />

presented here helps you see how the page and scripts in Listing 15-19b work.<br />

Listing 15-19a: The makeHot.htc Behavior Component<br />

<br />

<br />

<br />

<br />

<br />

var oldColor<br />

var hotColor = “red”<br />

function setHotColor(color) {<br />

hotColor = color<br />

}<br />

function makeHot() {<br />

if (event.srcElement == element) {<br />

oldColor = style.color<br />

runtimeStyle.color = hotColor<br />

}<br />

}<br />

function makeNormal() {<br />

if (event.srcElement == element) {<br />

runtimeStyle.color = oldColor<br />

}<br />

}<br />

<br />

The object to which the component is attached is a simple paragraph object,<br />

shown in Listing 15-19b. When the page loads, the behavior is not attached, so<br />

clicking the paragraph text has no effect.<br />

When you turn on the behavior by invoking the turnOn() function, the<br />

addBehavior() method attaches the code of the makeHot.htc component to the<br />

myP object. At this point, the myP object has one more property, one more method,<br />

and two more event handlers that are written to be made public by the component’s<br />

code. If you want the behavior to apply to more than one paragraph in the document,<br />

you have to invoke the addBehavior() method for each paragraph object.<br />

After the behavior file is instructed to start loading, the setInitialColor() function<br />

is called to set the new color property of the paragraph to the user’s choice from<br />

the SELECT list. But this can happen only if the component is fully loaded. Therefore,<br />

the function checks the readyState property of myP for completeness before invoking<br />

the component’s function. If IE is still loading the component, the function is<br />

invoked again in 500 milliseconds.<br />

51<br />

elementObject.addBehavior()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!