You can change the background color of a document at any time by setting document.bgColor to a new color. However, you cannot change any foreground colors -- fgColor (TEXT), alinkcolor, and so on -- if the page has already been written and is displayed on the screen. To change foreground colors, you need to close the input stream to the document [document.close()], open a new document [document.open()], and rewrite the entire page with new color values in the <BODY> tag set for that page.
Example
This page shows a button that, when clicked, changes the background color of the current page from white to purple or vice versa. Note that the JavaScript if() statement still requires the triplet with a pound sign (#) or stated as '#ffffff' rather than 'white'.
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
function reversecolor() {
// The if statement still requires the triplet.
if (document.bgColor == '#ffffff') {
document.bgColor = 'purple'}
else {
document.bgColor = 'white'
}
}
</SCRIPT>
<BODY bgColor = 'white'>
<FORM>
<INPUT TYPE = "button" value="click me" onclick="reversecolor()">
</FORM>
</BODY>
</HTML>

|
|
![]() |