mozilla

Revision 294620 of String.prototype.fontcolor()

  • Revision slug: JavaScript/Reference/Global_Objects/String/fontcolor
  • Revision title: fontcolor
  • Revision id: 294620
  • Created:
  • Creator: mathiasbynens
  • Is current revision? No
  • Comment

Revision Content

{{ Non-standard_header() }}

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
ECMAScript Edition none

Syntax

fontcolor(color)

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.

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.

Examples

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

{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/fontcolor", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/fontcolor" } ) }}

Revision Source

<p>{{ Non-standard_header() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Causes a string to be displayed in the specified color as if it were in a <code>&lt;font color="<em>color</em>"&gt;</code> tag.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>none</td>
    </tr>
  </tbody>
</table>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<p><code>fontcolor(<em>color</em>) </code></p>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>color</code></dt>
  <dd>
    A string expressing the color as a hexadecimal RGB triplet or as a string literal. String literals for color names are listed in the <a href="/en/JavaScript/Guide" title="en/JavaScript/Guide">Core JavaScript 1.5 Guide</a>.</dd>
</dl>
<h3 id="Description" name="Description">Description</h3>
<p>Use the <code>fontcolor</code> method with the <code>write</code> or <code>writeln</code> methods to format and display a string in a document. In server-side JavaScript, use the <code>write</code> function to display the string.</p>
<p>If you express color as a hexadecimal RGB triplet, you must use the format <code>rrggbb</code>. For example, the hexadecimal RGB values for salmon are red=FA, green=80, and blue=72, so the RGB triplet for salmon is "<code>FA8072</code>".</p>
<p>The <code>fontcolor</code> method overrides a value set in the <code>fgColor</code> property.</p>
<h3 id="Examples" name="Examples">Examples</h3>
<h4 id="Example:_Using_fontcolor" name="Example:_Using_fontcolor">Example: Using <code>fontcolor</code></h4>
<p>The following example uses the <code>fontcolor</code> method to change the color of a string:</p>
<pre>
var worldString="Hello, world";

document.write(worldString.fontcolor("maroon") +
   " is maroon in this line");
document.write("&lt;P&gt;" + worldString.fontcolor("salmon") +
   " is salmon in this line");
document.write("&lt;P&gt;" + worldString.fontcolor("red") +
   " is red in this line");

document.write("&lt;P&gt;" + worldString.fontcolor("8000") +
   " is maroon in hexadecimal in this line");
document.write("&lt;P&gt;" + worldString.fontcolor("FA8072") +
   " is salmon in hexadecimal in this line");
document.write("&lt;P&gt;" + worldString.fontcolor("FF00") +
   " is red in hexadecimal in this line");
</pre>
<p>The previous example produces the same output as the following HTML:</p>
<pre>
&lt;FONT COLOR="maroon"&gt;Hello, world&lt;/FONT&gt; is maroon in this line
&lt;P&gt;&lt;FONT COLOR="salmon"&gt;Hello, world&lt;/FONT&gt; is salmon in this line
&lt;P&gt;&lt;FONT COLOR="red"&gt;Hello, world&lt;/FONT&gt; is red in this line

&lt;P&gt;&lt;FONT COLOR="8000"&gt;Hello, world&lt;/FONT&gt;
is maroon in hexadecimal in this line
&lt;P&gt;&lt;FONT COLOR="FA8072"&gt;Hello, world&lt;/FONT&gt;
is salmon in hexadecimal in this line
&lt;P&gt;&lt;FONT COLOR="FF00"&gt;Hello, world&lt;/FONT&gt;
is red in hexadecimal in this line
</pre>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/fontcolor", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/fontcolor" } ) }}</p>
Revert to this revision