Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1129263 of TextEncoder

  • Revision slug: Web/API/TextEncoder
  • Revision title: TextEncoder
  • Revision id: 1129263
  • Created:
  • Creator: jpmedley
  • Is current revision? Yes
  • Comment

Revision Content

{{SeeCompatTable}}{{APIRef}}

TextEncoder takes a stream of code points as input and emits a stream of bytes. For a more scalable, non-native library, see StringView – a C-like representation of strings based on typed arrays.

Note: Firefox, Chrome and Opera had support for encoding types other than utf-8 (such as:  utf-16, iso-8859-2, koi8, cp1261, and gbk). As of Firebox 48 (ticket), Chrome 54 (ticket) and Opera 41, no other encoding types are available other than utf-8, in order to match the spec. In all cases, passing in an encoding type to the constructor will be ignored and a utf-8 TextEncoder will be created (Note: the TextDecoder still allows for other decoding types). 

There is a polyfill implementation to support all the legacy encoding in github.

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 utf-8 encoding.

Methods

The TextEncoder interface doesn't inherit any method.

{{domxref("TextEncoder.encode()")}}
Returns a Uint8Array containing utf-8 encoded text.

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 38 {{CompatGeckoDesktop("19.0")}} [1] {{CompatNo}} 25 {{CompatNo}}
Available in Web Workers 38 {{CompatGeckoDesktop("20.0")}} {{CompatNo}} 25 {{CompatNo}}
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoMobile("19.0")}} [1] {{CompatGeckoFxOS("19.0", "1.0.1")}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}}
Available in Web Workers {{CompatNo}} {{CompatGeckoMobile("20.0")}} {{CompatGeckoFxOS("20.0", "1.0.1")}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}}

[1] An earlier, and slightly different, version of the specification was implemented in Firefox 18.
[2] As of Firefox 48, Chrome 54 and Opera 41, no other encoding types are allowed other than utf-8. 

See also

Revision Source

<p>{{SeeCompatTable}}{{APIRef}}</p>

<p><strong>TextEncoder </strong>takes a stream of code points as input and emits a stream of bytes. For a more scalable, non-native library, see <a href="/en-US/Add-ons/Code_snippets/StringView" title="/en-US/docs/Web/JavaScript/Typed_arrays/StringView"><code>StringView</code> – a C-like representation of strings based on typed arrays</a>.</p>

<p><strong>Note</strong>: Firefox,&nbsp;Chrome and Opera had support for encoding types other than utf-8 (such as: &nbsp;utf-16,&nbsp;iso-8859-2, koi8, cp1261, and&nbsp;gbk). As of Firebox 48&nbsp;(<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1257877">ticket</a>), Chrome 54&nbsp;(<a href="https://www.chromestatus.com/feature/5630760492990464">ticket</a>) and Opera 41,&nbsp;no other encoding types are available other than utf-8, in order to match the <a href="https://www.w3.org/TR/encoding/#dom-textencoder">spec</a>. In all cases,&nbsp;passing in an encoding type to the constructor&nbsp;will be ignored and a utf-8 TextEncoder will be created (Note: the <a href="/en-US/docs/Web/API/TextDecoder">TextDecoder</a> still allows for other decoding types).&nbsp;</p>

<p>There is a polyfill implementation to support all the legacy encoding in <a href="https://github.com/inexorabletash/text-encoding">github</a>.</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 utf-8 encoding.</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>&nbsp;containing utf-8&nbsp;encoded&nbsp;text.</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>38</td>
   <td>{{CompatGeckoDesktop("19.0")}} [1]</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Available in Web Workers</td>
   <td>38</td>
   <td>{{CompatGeckoDesktop("20.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</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><strong>Firefox OS</strong></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")}} [1]</td>
   <td>{{CompatGeckoFxOS("19.0", "1.0.1")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Available in Web Workers</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("20.0")}}</td>
   <td>{{CompatGeckoFxOS("20.0", "1.0.1")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] An earlier, and slightly different, version of the specification was implemented in Firefox 18.<br />
 [2] As of Firefox 48, Chrome 54&nbsp;and Opera 41, no other encoding types are allowed other than utf-8.&nbsp;</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>The {{domxref("TextDecoder")}} interface describing the inverse operation.</li>
 <li><a href="/en-US/Add-ons/Code_snippets/StringView" title="/en-US/docs/Web/JavaScript/Typed_arrays/StringView"><code>StringView</code></a> – a C-like representation of strings based on typed arrays</li>
 <li>A <a href="https://github.com/inexorabletash/text-encoding" title="https://code.google.com/p/stringencoding/">shim</a> allowing to use this interface in browsers that don't support it.</li>
 <li><a href="/en-US/docs/Components.utils.importGlobalProperties">Components.utils.importGlobalProperties</a></li>
</ul>
Revert to this revision