Last active
August 29, 2015 14:07
-
-
Save carmichaelize/29b9d3e1138e387b56c1 to your computer and use it in GitHub Desktop.
JavaScript Base64 Encode/Decode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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