语法
js
new TextDecoder()
new TextDecoder(utfLabel)
new TextDecoder(utfLabel, options)
参数
utfLabel可选-
一个字符串,默认是
"utf-8"。可以是任意有效的编码。 options可选-
一个具有属性的对象:
fatal-
一个布尔值,表示在解码无效数据时,
TextDecoder.decode()方法是否必须抛出TypeError。默认是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 }); // Allows TypeError exception to be thrown.
const textDecoder4 = new TextDecoder("iso-2022-cn"); // Throw a RangeError exception.
规范
| 规范 |
|---|
| Encoding > # ref-for-dom-textdecoder① > |
浏览器兼容性
参见
- 属于
TextDecoder接口。