--- title: WindowOrWorkerGlobalScope.atob() slug: Web/API/atob tags: - API - Base64 - DOM - WindowOrWorkerGlobalScope - atob - 参考 - 方法 translation_of: Web/API/WindowOrWorkerGlobalScope/atob original_slug: 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> <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>