{{APIRef("HTML DOM")}}
Creates a base-64 encoded ASCII string from a "string" of binary data.
Please note that this is not suitable for raw Unicode strings! See Unicode section below.
Syntax
var encodedData = window.btoa(stringToEncode);
Example
var encodedData = window.btoa("Hello, world"); // encode a string var decodedData = window.atob(encodedData); // decode the string
Notes
You can use this method to encode data which may otherwise cause communication problems, transmit it, then use the {{domxref("WindowBase64.atob","window.atob()")}}
method to decode the data again. For example, you can encode control characters such as ASCII values 0 through 31.
btoa()
is also available to XPCOM components implemented in JavaScript, even though window
is not the global object in components.
Unicode Strings
In most browsers, calling window.btoa()
on a Unicode string will cause a Character Out Of Range exception.
To avoid this, consider this pattern, noted by Johan Sundström:
function utf8_to_b64(str) { return window.btoa(unescape(encodeURIComponent(str))); } function b64_to_utf8(str) { return decodeURIComponent(escape(window.atob(str))); } // Usage: utf8_to_b64('✓ à la mode'); // JTI1dTI3MTMlMjUyMCUyNUUwJTI1MjBsYSUyNTIwbW9kZQ== b64_to_utf8('JTI1dTI3MTMlMjUyMCUyNUUwJTI1MjBsYSUyNTIwbW9kZQ=='); // "✓ à la mode" utf8_to_b64('I \u2661 Unicode!'); // SSUyNTIwJTI1dTI2NjElMjUyMFVuaWNvZGUlMjUyMQ== b64_to_utf8('SSUyNTIwJTI1dTI2NjElMjUyMFVuaWNvZGUlMjUyMQ=='); // "I ♡ Unicode!"
A better, more faithful and less expensive solution is to convert the DOMString
to a UTF-8 encoded string passing for typed arrays. In order to do this, please, read this paragraph.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', '#dom-windowbase64-btoa', 'WindowBase64.btoa()')}} | {{Spec2('HTML WHATWG')}} | No change since the latest snapshot, {{SpecName("HTML5.1")}}. |
{{SpecName('HTML5.1', '#dom-windowbase64-btoa', 'WindowBase64.btoa()')}} | {{Spec2('HTML5.1')}} | Snapshot of {{SpecName("HTML WHATWG")}}. No change. |
{{SpecName("HTML5 W3C", "#dom-windowbase64-btoa", "WindowBase64.btoa()")}} | {{Spec2('HTML5 W3C')}} | Snapshot of {{SpecName("HTML WHATWG")}}. Creation of WindowBase64 (properties where on the target before it). |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoDesktop(1)}}[1] | 10 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatGeckoMobile(1)}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatVersionUnknown}} |
[1] btoa()
is also available to XPCOM components implemented in JavaScript, even though window
is not the global object in components.
See also
- Base64 encoding and decoding
data
URIs- {{domxref("WindowBase64.atob","window.atob()")}}
- Components.utils.importGlobalProperties