Document Color Properties

The bgColor , fgColor, linkColor, alinkColor, and vlinkColor properties of the Document object specify foreground, background, and link colors for the document. They are read/write properties, but they can be set only before the <body> tag is parsed. You can set them dynamically with JavaScript code in the <head> section of a document, or you can set them statically as attributes of the <body> tag, but you cannot set them elsewhere. The exception to this rule is the bgColor property. In many browsers, you can set this property at any time; doing so causes the background color of the browser window to change.[49] Other than bgColor, the color properties of the Document object merely expose attributes of the <body> tag and are basically uninteresting.

Each of these color properties has a string value. To set a color, you can use one of the predefined HTML color names, or you can specify the color as red, green, and blue color values, expressed as a string of six hexadecimal digits in the form # RRGGBB. You may recall that Example 13-7 set the bgcolor attribute of the <body> tag to a color string expressed in this fashion.

In the W3C DOM standard, the color properties of the Document object are deprecated in favor of properties of the Element object that represents the <body> tag. Furthermore, the HTML 4 standard deprecates the color attributes of the <body> tag in favor of CSS style sheets. What this means is that you probably should not write scripts ...

Get JavaScript: The Definitive Guide, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.