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

TextEncoder 将输入的代码流指向一个字节流并输出. 现提供一个可扩展的非原生库, 参见 StringView – a C-like representation of strings based on typed arrays.

Note: Firefox, Chrome and Opera 已经支持utf-8以外的编码格式 (例如:  utf-16, iso-8859-2, koi8, cp1261, and gbk). 从Firefox 48 (ticket), Chrome 54 (ticket) 和 Opera 41开始, 为了符合规范,除了utf-8以外的编码格式都不可用,在任何情况下,将编码类型传递给构造函数将被忽略,并且将创建一个utf-8 TextEncoder (Note: TextDecoder 仍然允许其他解码类型). 

There is a polyfill implementation to support all the legacy encoding in github.

属性

TextEncoder 接口不会继承任何属性。

TextEncoder.encoding 只读
该属性返回一个包含编码器名称的 DOMString , 用来描述TextEncoder将会使用的方法的字符串。

构造

TextEncoder()
返回一个新构造的TextEncoder,它将默认使用utf-8编码生成一个字节流。

方法

TextEncoder 接口不会继承任何方法。

TextEncoder.encode()
返回包含使用此特定TextEncoder对象的方法编码的文本的Uint8Array。

标准

Specification Status Comment
Encoding
TextEncoder
Living Standard Initial definition.

浏览器支持

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari No support NoWebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS No support NoSamsung Internet Android ?
Available in Web Workers
Experimental
Chrome Full support 38Edge ? Firefox Full support 20IE No support NoOpera Full support 25Safari No support NoWebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 20Opera Android ? Safari iOS No support NoSamsung Internet Android ?
TextEncoder() constructor
Experimental
Chrome Full support 53
Notes
Full support 53
Notes
Notes Does not accept parameters. Supports only utf-8 encoding.
No support 38 — 53
Notes
Notes Throws RangeError exception for unknown encoding types.
Edge ? Firefox Full support 48
Notes
Full support 48
Notes
Notes The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
No support 38 — 48
Notes
Notes If the encoding type label argument is invalid, then a RangeError exception is thrown.
No support 19 — 38
Notes
Notes If the encoding type label argument is invalid, then a TypeError exception is thrown.
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari No support NoWebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 48
Notes
Full support 48
Notes
Notes The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
No support 38 — 48
Notes
Notes If the encoding type label argument is invalid, then a RangeError exception is thrown.
No support 19 — 38
Notes
Notes If the encoding type label argument is invalid, then a TypeError exception is thrown.
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS No support NoSamsung Internet Android ?
encoding
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari No support NoWebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS No support NoSamsung Internet Android ?
encode
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari No support NoWebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

另见

文档标签和贡献者

此页面的贡献者: fscholz, Taoja, teoli
最后编辑者: fscholz,