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.

Note<br />

Chapter 5 ✦ Body Text Objects (Chapter 19)<br />

three FONT object properties, and their NAME attributes are strategically assigned<br />

the names of the properties (as you see in a moment). VALUE attributes for OPTION<br />

elements contain strings that are to be assigned to the various properties. Each<br />

SELECT element invokes the same setFontAttr() function, passing a reference to<br />

itself so that the function can inspect details of the element.<br />

The first task of the setFontAttr() function is to make sure that only browsers<br />

capable of treating the FONT element as an object get to the meat of the function.<br />

The test for the existence of document.all and the myFONT element blocks all<br />

older browsers from changing the font characteristics. As the page loads, the<br />

document.all property is set for NN6 by using a variation of the normalization<br />

technique described in Chapter 14 of the <strong>JavaScript</strong> <strong>Bible</strong>.<br />

For suitably equipped browsers, the function next extracts the string from the<br />

value property of the SELECT object that was passed to the function. If a selection<br />

is made (meaning other than the first, empty one), then the single nested statement<br />

uses the setAttribute() method to assign the value to the attribute whose name<br />

matches the name of the SELECT element.<br />

An odd bug in IE5/Mac doesn’t let the rendered color change when changing the<br />

color property. But the setting is valid, as proven by selecting any of the other<br />

two property choices.<br />

Listing 19-1: Controlling FONT Object Properties<br />

<br />

<br />

FONT Object Properties<br />

<br />

// document.all normalization trick for NN6<br />

if (navigator.appName == “Netscape” && parseInt(navigator.appVersion) >= 5) {<br />

document.all = document.getElementsByTagName(“*”)<br />

}<br />

// one function does all!<br />

function setFontAttr(select) {<br />

if (document.all && document.all.myFONT) {<br />

var choice = select.options[select.selectedIndex].value<br />

if (choice) {<br />

document.all.myFONT.setAttribute(select.name, choice)<br />

}<br />

}<br />

}<br />

<br />

<br />

<br />

Font Object Properties<br />

<br />

Continued<br />

267<br />

FONT.color

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

Saved successfully!

Ooh no, something went wrong!