aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/windowbase64/atob/index.html
blob: 2892e403d4f3967eab0d88d947e63fad5108c5cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
title: WindowOrWorkerGlobalScope.atob()
slug: Web/API/WindowBase64/atob
tags:
  - API
  - Base64
  - DOM
  - WindowOrWorkerGlobalScope
  - atob
  - 参考
  - 方法
translation_of: Web/API/WindowOrWorkerGlobalScope/atob
---
<p>{{APIRef("HTML DOM")}}</p>

<p><strong><code>WindowOrWorkerGlobalScope.atob()</code></strong> 对经过 base-64 编码的字符串进行解码。你可以使用 {{domxref("WindowBase64.btoa","window.btoa()")}} 方法来编码一个可能在传输过程中出现问题的数据,并且在接受数据之后,使用 atob() 方法再将数据解码。例如:你可以编码、传输和解码操作各种字符,比如 0-31 的 ASCII 码值。</p>

<p>关于针对 Unicode 或者 UTF-8 的应用方面,请查看 <a href="/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding#The_.22Unicode_Problem.22">this note at Base64 encoding and decoding</a><a href="/en-US/docs/Web/API/window.btoa#Unicode_Strings"><code>btoa()</code> 的备注</a></p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">var decodedData = scope.atob(<em>encodedData</em>);</pre>

<h3 id="异常">异常</h3>

<p>如果传入字符串不是有效的 base64 字符串,比如其长度不是 4 的倍数,则抛出{{jsxref("DOMException")}}</p>

<h2 id="示例">示例</h2>

<pre class="brush:js">let encodedData = window.btoa("Hello, world"); // 编码
let decodedData = window.atob(encodedData);    // 解码
</pre>

<h2 id="Specification" name="Specification">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No change since the latest snapshot, {{SpecName("HTML5.1")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', '#dom-windowbase64-atob', 'WindowBase64.atob()')}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>Snapshot of {{SpecName("HTML WHATWG")}}. No change.</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5 W3C", "#dom-windowbase64-atob", "WindowBase64.atob()")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>Snapshot of {{SpecName("HTML WHATWG")}}. Creation of <code>WindowBase64</code> (properties were on the target before it).</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Compatibility" name="Browser_Compatibility">浏览器兼容性</h2>

<div class="hidden">
<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>

<p>{{Compat("api.WindowOrWorkerGlobalScope.atob")}}</p>

<h2 id="See_also" name="See_also">参见</h2>

<ul>
 <li><a href="/Web/API/WindowBase64/Base64_encoding_and_decoding">Base64 encoding and decoding</a></li>
 <li><a href="/en-US/docs/data_URIs"><code>data</code> URIs</a></li>
 <li>{{domxref("WindowOrWorkerGlobalScope.btoa","window.btoa()")}}</li>
 <li><a href="/en-US/docs/Components.utils.importGlobalProperties">Components.utils.importGlobalProperties</a></li>
</ul>