Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active August 29, 2015 14:07
Show Gist options
  • Save carmichaelize/29b9d3e1138e387b56c1 to your computer and use it in GitHub Desktop.
Save carmichaelize/29b9d3e1138e387b56c1 to your computer and use it in GitHub Desktop.
JavaScript Base64 Encode/Decode
//Encode
function encodeBase64( string ){
try{
if( typeof string == 'object'){
throw 'error';
}
return window.btoa(unescape(encodeURIComponent( string )));
} catch(e){
return false;
}
}
//Decode
function decodeBase64( string ){
try {
if( typeof string != 'string'){
throw 'error';
}
return decodeURIComponent(escape(window.atob( string )));
} catch(e){
return false;
}
}
var code = encodeBase64('Hello. How are You?');
console.log( code );
var string = decodeBase64(code);
console.log( string );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment