Java Reference
In-Depth Information
You do the same with
colorDepth
values of
8
,
15
, and
16
, setting the background color to blue as
follows:
case 8:
case 15:
case 16:
document.bgColor = "blue";
break;
Finally, you do the same for
colorDepth
values of
24
and
32
, setting the background color to sky blue:
case 24:
case 32:
document.bgColor = "skyblue";
break;
You end the
switch
statement with a
default
case, just in case the other
case
statements did not
match. In this
default
case, you again set the background color to white:
default:
document.bgColor = "white";
}
In the next bit of script, you use the
document
object's
write()
method, something you've been using in
these examples for a while now. You use it to write to the document—that is, the page—the number of
bits at which the color depth is currently set, as follows:
document.write("Your screen supports " + colorDepth +
"bit color")
You've already been using the
document
object in the examples throughout the topic. You used its
bgColor
property in Chapter 1 to change the background color of the page, and you've also made good
use of its
write()
method in the examples to write HTML and text out to the page.
Now let's look at some of the slightly more complex properties of the
document
object. These
properties have in common the fact that they all contain collections. The first one you look at is a
collection containing an object for each image in the page.
the images Collection
As you know, you can insert an image into an HTML page using the following tag:
<img alt="USA" name="myImage" src="usa.gif" />
The browser makes this image available for you to manipulate with JavaScript by creating an
img
object for it with the name
myImage
. In fact, each image on your page has an
img
object created
for it.
Each of the
img
objects in a page is stored in the
images
collection, which is a property of the
document
object. You use this, and other collections, as you would an array. The first image on