The WindowOrWorkerGlobalScope.atob()
function decodes a string of data which has been encoded using base-64 encoding. You can use the btoa()
method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob()
method to decode the data again. For example, you can encode, transmit, and decode control characters such as ASCII values 0 through 31.
For use with Unicode or UTF-8 strings, see this note at Base64 encoding and decoding and this note at btoa()
.
Syntax
var decodedData = scope.atob(encodedData);
Throws
Throws a DOMException
if encodedData is not valid base64.
Example
var encodedData = window.btoa('Hello, world'); // encode a string var decodedData = window.atob(encodedData); // decode the string
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'WindowOrWorkerGlobalScope.atob()' in that specification. |
Living Standard | Method moved to the WindowOrWorkerGlobalScope mixin in the latest spec. |
HTML 5.1 The definition of 'WindowBase64.atob()' in that specification. |
Recommendation | Snapshot of HTML Living Standard. No change. |
HTML5 The definition of 'WindowBase64.atob()' in that specification. |
Recommendation | Snapshot of HTML Living Standard. Creation of WindowBase64 (properties were on the target before it). |
Polyfill
// From https://github.com/MaxArt2501/base64-js/blob/master/base64.js (function() { if (window.atob) { // Some browsers' implementation of atob doesn't support whitespaces // in the encoded string (notably, IE). This wraps the native atob // in a function that strips the whitespaces. // The original function can be retrieved in atob.original try { window.atob(" "); } catch (e) { window.atob = (function(atob) { var func = function(string) { return atob(String(string).replace(/[\t\n\f\r ]+/g, "")); }; func.original = atob; return func; })(root.atob); } return; } // base64 character set, plus padding character (=) var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // Regular expression to check formal correctness of base64 encoded strings b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/; window.atob = function(string) { // atob can work with strings with whitespaces, even inside the encoded part, // but only \t, \n, \f, \r and ' ', which can be stripped. string = String(string).replace(/[\t\n\f\r ]+/g, ""); if (!b64re.test(string)) throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."); // Adding the padding if missing, for semplicity string += "==".slice(2 - (string.length & 3)); var bitmap, result = "", r1, r2, i = 0; for (; i < string.length;) { bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12 | (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++))); result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255) : r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255) : String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255); } return result; }; })()
Browser compatibility
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.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
atob | Chrome Full support Yes | Edge Full support Yes | Firefox
Full support
1
| IE Full support 10 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
4
| Opera Android ? | Safari iOS Full support Yes | Samsung Internet Android ? |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.