TextDecoder()

The TextDecoder() constructor returns a newly created TextDecoder object for the encoding specified in parameter.

If the value for utfLabel is unknown, or is one of the two values leading to a 'replacement' decoding algorithm ( "iso-2022-cn" or "iso-2022-cn-ext"), a DOMException with the "TypeError" value is thrown.

Syntax

decoder = new TextDecoder(utfLabel, options);

Parameters

utfLabelOptional

Is a DOMString, defaulting to "utf-8", containing the label of the encoder. This may be any valid label.

optionsOptional

Is a TextDecoderOptions dictionary with the property:

fatal

A Boolean flag indicating if the TextDecoder.decode() method must throw a DOMException with the "EncodingError" value when an coding error is found. It defaults to false.

Example

var textDecoder1 = new TextDecoder("iso-8859-2");
var textDecoder2 = new TextDecoder();
var textDecoder3 = new TextDecoder("csiso2022kr", {fatal: true}); // Allows EncodingError exception to be thrown.
var textDecoder4 = new TextDecoder("iso-2022-cn"); // Throw a TypeError exception.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
TextDecoder
38
79
19
18
Implemented a slightly different version of the spec.
No
25
10.1
38
38
19
18
Implemented a slightly different version of the spec.
25
10.3
3.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/TextDecoder