← 返回首页
TextDecoder() - Web API | MDN

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

TextDecoder()

基线 广泛可用

自 2020年1月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

TextDecoder() 构造函数使用参数中指定的编码返回一个新创建的 TextDecoder 对象。

本文内容

语法

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①

浏览器兼容性

启用 JavaScript 以查看此浏览器兼容性表。

参见