Base64 Encoding and Decoding in Client-Side Javascript [Duplicate]
Are There Any Methods in Javascript That Could Be Used to Encode and Decode a String Using Base64 Encoding? 1 16 Answers Some Browsers Such as Firefox, Chrome...
Are there any methods in JavaScript that could be used to encode and decode a string using base64 encoding?
16 Answers
Some browsers such as Firefox, Chrome, Safari, Opera and IE10+ can handle Base64 natively. Take a look at this Stackoverflow question. It's using btoa() and atob() functions.
For server-side JavaScript (Node), you can use Buffers to decode.
If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:
(Archive)
With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.
Must Read
Internet Explorer 10+
// Define the string
var string = 'Hello World!';
// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"
// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"