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 RangeError is thrown.

Syntax

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 TypeError 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 TypeError exception to be thrown.
var textDecoder4 = new TextDecoder("iso-2022-cn"); // Throw a RangeError exception.

Specifications

Specification
Encoding
# ref-for-dom-textdecoder①

Browser compatibility

BCD tables only load in the browser

See also