TextDecoder

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

TextDecoder是一个特定解码器的方法,诸如utf-8iso-8859-2koi8cp1261gbk,等的字符编码,解码器将字节流作为输入并发出代码点流 。有关更多的非原生可扩展库,请参见StringView–基于类型数组的字符串的C类表示

例子

用类型化数组表示文本

本示例展示如何解码中文/日语字符,用五个不同的数组类型表示 Uint8Array, Int8Array, Uint16Array, Int16Array, Int32Array

let utf8decoder = new TextDecoder(); // default 'utf-8' or 'utf8'

let u8arr = new Uint8Array([240, 160, 174, 183]);
let i8arr = new Int8Array([-16, -96, -82, -73]);
let u16arr = new Uint16Array([41200, 47022]);
let i16arr = new Int16Array([-24336, -18514]);
let i32arr = new Int32Array([-1213292304]);

console.log(utf8decoder.decode(u8arr));
console.log(utf8decoder.decode(i8arr));
console.log(utf8decoder.decode(u16arr));
console.log(utf8decoder.decode(i16arr));
console.log(utf8decoder.decode(i32arr));

处理非UTF8文本

在此示例中,我们对俄语文本“Привет,мир!”( "Hello, world.")进行解码。在我们的 TextDecoder() 构造函数中,我们指定Windows-1251字符编码,适用于西里尔字母。

let win1251decoder = new TextDecoder('windows-1251');
let bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
console.log(win1251decoder.decode(bytes)); // Привет, мир!

构造函数

TextDecoder()
返回一个新构造函数TextDecoder,它使用参数中指定的解码方法生成代码点流。

属性

TextDecoder不继承任何属性。

TextDecoder.prototype.encoding只读
DOMString所包含的解码器的名称,表示TextDecoder所使用的解码方法的字符串。
TextDecoder.prototype.fatal只读
布尔值,Boolean,是否显示致命错误。
TextDecoder.prototype.ignoreBOM 只读
布尔值,Boolean,是否忽略BOM标记。

方法

TextDecoder接口不继承任何方法

TextDecoder.prototype.decode()
返回一个DOMString,其中包含使用特定TextDecoder对象的方法解码的文本

规范

规格 状态 评论
Encoding
TextDecoder
Living Standard 初始定义。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
TextDecoderChrome Full support 38Edge ? Firefox Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
Opera Android Full support YesSafari iOS Full support 10.3Samsung Internet Android Full support Yes
TextDecoder() constructorChrome Full support 38Edge ? Firefox Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
Opera Android ? Safari iOS Full support 10.3Samsung Internet Android ?
decodeChrome Full support 38Edge ? Firefox Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
Opera Android Full support YesSafari iOS Full support 10.3Samsung Internet Android Full support Yes
encodingChrome Full support 38Edge ? Firefox Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 19
Full support 19
Partial support 18
Notes
Notes Implemented a slightly different version of the spec.
Opera Android Full support YesSafari iOS Full support 10.3Samsung Internet Android Full support Yes
fatalChrome Full support YesEdge ? Firefox Full support YesIE No support NoOpera Full support YesSafari Full support 10.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support 10.3Samsung Internet Android Full support Yes
ignoreBOMChrome Full support YesEdge ? Firefox Full support YesIE No support NoOpera Full support YesSafari Full support 10.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support 10.3Samsung Internet Android Full support Yes
Available in workersChrome Full support 38Edge ? Firefox Full support 20IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 20Opera Android ? Safari iOS Full support 10.3Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

相关链接