Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:String:fontcolor

From MDC

Non-standard

Contents

[edit] Summary

Causes a string to be displayed in the specified color as if it were in a <FONT COLOR="color"> tag.

Method of String
Implemented in: JavaScript 1.0, NES2.0

[edit] Syntax

fontcolor(color)

[edit] Parameters

color 
A string expressing the color as a hexadecimal RGB triplet or as a string literal. String literals for color names are listed in the Core JavaScript 1.5 Guide.

[edit] Description

Use the fontcolor method with the write or writeln methods to format and display a string in a document. In server-side JavaScript, use the write function to display the string.

If you express color as a hexadecimal RGB triplet, you must use the format rrggbb. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "FA8072".

The fontcolor method overrides a value set in the fgColor property.

[edit] Examples

[edit] Example: Using fontcolor

The following example uses the fontcolor method to change the color of a string:

var worldString="Hello, world"

document.write(worldString.fontcolor("maroon") +
   " is maroon in this line")
document.write("<P>" + worldString.fontcolor("salmon") +
   " is salmon in this line")
document.write("<P>" + worldString.fontcolor("red") +
   " is red in this line")

document.write("<P>" + worldString.fontcolor("8000") +
   " is maroon in hexadecimal in this line")
document.write("<P>" + worldString.fontcolor("FA8072") +
   " is salmon in hexadecimal in this line")
document.write("<P>" + worldString.fontcolor("FF00") +
   " is red in hexadecimal in this line")

The previous example produces the same output as the following HTML:

<FONT COLOR="maroon">Hello, world</FONT> is maroon in this line
<P><FONT COLOR="salmon">Hello, world</FONT> is salmon in this line
<P><FONT COLOR="red">Hello, world</FONT> is red in this line

<P><FONT COLOR="8000">Hello, world</FONT>
is maroon in hexadecimal in this line
<P><FONT COLOR="FA8072">Hello, world</FONT>
is salmon in hexadecimal in this line
<P><FONT COLOR="FF00">Hello, world</FONT>
is red in hexadecimal in this line