TextEncoder

  • Revision slug: Web/API/TextEncoder
  • Revision title: TextEncoder
  • Revision id: 456651
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{SeeCompatTable}}

The TextEncoder interface represents an encoder for a specific method, that is a specific character encoding, like utf-8, iso-8859-2, koi8, cp1261, gbk, ... An encoder takes a stream of code points as input and emits a stream of bytes.

Properties

The TextEncoder interface doesn't inherit any property.

{{domxref("TextEncoder.encoding")}} {{readonlyInline}}
Is a {{domxref("DOMString")}} containing the name of the encoder, that is a string describing the method the TextEncoder will use.

Constructor

{{domxref("TextEncoder.TextEncoder", "TextEncoder()")}}
Returns a newly constructed TextEncoder that will generate a byte stream with the encoding method specified in parameters.

Methods

The TextEncoder interface doesn't inherit any method.

{{domxref("TextEncoder.encode()")}}
Returns a Uint8Array containing the text encoded with the specific method for that TextEncoder object.

Specifications

Specification Status Comment
{{SpecName('Encoding', '#interface-textencoder', 'TextEncoder')}} {{Spec2('Encoding')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatNo}} {{CompatGeckoDesktop("19.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoMobile("19.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • The {{domxref("TextDecoder")}} interface describing the inverse operation.
  • A shim allowing to use this interface in browsers that don't support it.

Revision Source

<p>{{SeeCompatTable}}</p>
<p>The <code><strong>TextEncoder</strong></code> interface represents an encoder for a specific method, that is a specific character encoding, like <code>utf-8</code>, <code>iso-8859-2</code>, <code>koi8</code>, <code>cp1261</code>, <code>gbk</code>, ... An encoder takes a stream of code points as input and emits a stream of bytes.</p>
<h2 id="Properties">Properties</h2>
<p><em>The <code>TextEncoder</code> interface doesn't inherit any property.</em></p>
<dl>
  <dt>
    {{domxref("TextEncoder.encoding")}} {{readonlyInline}}</dt>
  <dd>
    Is a {{domxref("DOMString")}} containing the name of the encoder, that is a string describing the method the <code>TextEncoder</code> will use.</dd>
</dl>
<h2 id="Constructor">Constructor</h2>
<dl>
  <dt>
    {{domxref("TextEncoder.TextEncoder", "TextEncoder()")}}</dt>
  <dd>
    Returns a newly constructed <code>TextEncoder</code> that will generate a byte stream with the encoding method specified in parameters.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<p><em><em>The <code>TextEncoder</code> interface doesn't inherit any </em>method</em>.</p>
<dl>
  <dt>
    {{domxref("TextEncoder.encode()")}}</dt>
  <dd>
    Returns a <a href="/en-US/docs/Web/JavaScript/Typed_arrays/Uint8Array" title="/en-US/docs/Web/JavaScript/Typed_arrays/Uint8Array"><code>Uint8Array</code></a> containing the text encoded with the specific method for that <code>TextEncoder</code> object.</dd>
</dl>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
    <tr>
      <td>{{SpecName('Encoding', '#interface-textencoder', 'TextEncoder')}}</td>
      <td>{{Spec2('Encoding')}}</td>
      <td>Initial definition.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoDesktop("19.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoMobile("19.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li>The {{domxref("TextDecoder")}} interface describing the inverse operation.</li>
  <li>A <a href="http://code.google.com/p/stringencoding/" title="http://code.google.com/p/stringencoding/">shim</a> allowing to use this interface in browsers that don't support it.</li>
</ul>
Revert to this revision