这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

TextDecoder.prototype.decode() 方法返回 DOMString containing the text, given in parameters, decoded with the specific method for that TextDecoder 对象。

句法

b1 = decoder.decode(buffer, options);
b2 = decoder.decode(buffer);
b3 = decoder.decode();
					

参数

buffer 可选
Is either an ArrayBuffer ArrayBufferView containing the text to decode.
选项 可选
TextDecodeOptions dictionary with the property:
stream
A 布尔 flag indicating that additional data will follow in subsequent calls to decode(). Set to true if processing the data in chunks, and false for the final chunk or if the data is not chunked. It defaults to false.

范例

This example encodes and decodes the euro symbol, €.

HTML

<p>Encoded value: <span id="encoded-value"></span></p>
<p>Decoded value: <span id="decoded-value"></span></p>
								

JavaScript

const encoder = new TextEncoder();
const array = encoder.encode('€'); // Uint8Array(3) [226, 130, 172]
document.getElementById('encoded-value').textContent = array;
const decoder = new TextDecoder();
const str = decoder.decode(array); // String "€"
document.getElementById('decoded-value').textContent = str;
								

结果

规范

规范 状态 注释
编码
The definition of 'TextDecoder.decode()' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
decode Chrome 38 Edge ≤79 Firefox 19
19
部分支持 18 注意事项
Implemented a slightly different version of the spec.
IE 不支持 No Opera 25 Safari 10.1 WebView Android 38 Chrome Android 38 Firefox Android 19
19
部分支持 18 注意事项
Implemented a slightly different version of the spec.
Opera Android Yes Safari iOS 10.3 Samsung Internet Android 3.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 编码 API
  2. TextDecoder
  3. 构造函数
    1. TextDecoder()
  4. 特性
    1. encoding
  5. 方法
    1. decode()
  6. Related pages for Encoding API
    1. TextEncoder

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1