Mo. Mo. - 11 months ago 104
Javascript Question

Base64 encoding and decoding in client-side Javascript

Are there any methods in JavaScript that could be used to encode and decode a string using base64 encoding?

Answer Source

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, there's a btoa package for Node.JS.

If you are going for a cross-browser solution, there are existing libraries like CryptoJS or code like:

With the latter, you need to thoroughly test the function for cross browser compatibility. And error has already been reported.