Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Document Properties - Page 6

December 7, 2001

Now that we've conquered the logic of if and else, let's look at the actual code for these pages. Notice that not only the graphics and text are different, but the background color and the text color as well.

If the hour is between 4 and 16, the script changes the background color to white and the text color to black, displays the graphic photo-day.jpg, and writes the text about skiing powder. If the hour is not between 4 and 16, the else statement tells the script to change the background color to black and the text color to white, display the graphic photo-night.jpg, and write the text about having a drink.

Changing the colors involves setting two properties of the current Web page, or, as JavaScript knows it, the document object. The properties of the document object describe different characteristics of the object. Assigning a hexadecimal value to the document.bgColor property changes the background color; document.fgColor changes the text color. You can change the background color on the fly, at any time during the document's existence, but in some browsers, the property for the text color can only be changed when the document is initially displayed, so it is best not to change this after the document has been displayed. We'll learn how to change the color and appearance of text on the fly in Chapter 9.

The JavaScript Tree

The document object and its bgColor property are separated by a period. Thus, document.bgColor refers to the background color property of the document. That seems simple enough, but what do you make of this?

document.mailform.address.value

It refers to the value of the address element of a form called mailform, which is in the document. Clear as mud? It makes a little more sense if you think of it as a tree.

In general, JavaScript organizes all parts of the browser window and all elements on a page (e.g., forms and images) like a tree. First, there's a main object (the trunk), then there are objects off the main object (branches), and finally there are methods and properties off those objects (leaves). As you'll see in later chapters, this is referred to technically as the document object model, but the tree metaphor is an easier way to think about it for now.

The main object, the trunk, is always the current browser window, referred to as window. There are many branches off the browser window: the page currently displayed in the window, called document, the current location of the window, called location, the history of all the visited pages of the window, called history, and the value of the status bar, status. Inside each of these branches, you'll find more objects. For example, the document object contains all the elements on a given page: forms, images, and links are all reflected as objects. To illustrate this concept, the figure shows a small "slice" of the JavaScript tree.

As you can see, the tree begins with the browser window and branches off from there. (This is by no means the complete JavaScript tree, which would take up about 20 pages.) Whenever you want to access something in the JavaScript tree, you have to "climb" up to it. To better understand this, let's create our own example of "climbing the tree" using a simple HTML document:

<html>
<body>
<form name="mailform">
<input type="text" name="address">
</form>
</body>
</html>

The names of the form and the text input field specified in their HTML tags correspond to the names used by JavaScript. Here we have a form named mailform that contains a text input field named address. To get the text entered by the user, you need to ask for the value of address. That's the value of address in form mailform in the document in the window:

window.document.mailform.address.value

Because it is very common to access elements in the current page, JavaScript lets you take a shortcut and start climbing the tree at document. Thus, you can also access the text of the address field as:

document.mailform.address.value

Displaying The Page - Page 5
Designing with JavaScript, 2nd Edition
What Else Can You Do With The Date? - Page 7


Up to => Home / Authoring / JavaScript / Design




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers