TextDecoder: TextDecoder() コンストラクター

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

TextDecoder() コンストラクターは、引数で指定されたエンコーディング用に新しく作成された TextDecoder オブジェクトを返します。

構文

js
new TextDecoder()
new TextDecoder(label)
new TextDecoder(label, options)

引数

label 省略可

文字列で、既定値は "utf-8" です。 これは何れかの有効なラベルです。

options 省略可

次のプロパティを持つオブジェクトです。

fatal

論理値で、無効なデータをデコードした場合に TextDecoder.decode() メソッドが TypeError を発生させるかどうかを示します。 既定値では false であり、これはデコードする際に不正なデータを置換文字で置き換えることを意味します。

ignoreBOM

論理値で、バイトオーダーマークを無視するか否かを示します。 既定値は false です。

例外

RangeError

label の値が未知の場合、または 'replacement' デコードアルゴリズムにつながる値のいずれか("iso-2022-cn" または "iso-2022-cn-ext")になっている場合に発生します。

js
const textDecoder1 = new TextDecoder("iso-8859-2");
const textDecoder2 = new TextDecoder();
const textDecoder3 = new TextDecoder("csiso2022kr", { fatal: true }); // TypeError 例外を発生できるようにします。
const textDecoder4 = new TextDecoder("iso-2022-cn"); // RangeError 例外が発生します。

仕様書

Specification
Encoding
# ref-for-dom-textdecoder①

ブラウザーの互換性

BCD tables only load in the browser

関連情報