Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created February 25, 2016 20:43
Show Gist options
  • Save JDMcKinstry/9b18a615ab07db50a672 to your computer and use it in GitHub Desktop.
Save JDMcKinstry/9b18a615ab07db50a672 to your computer and use it in GitHub Desktop.
Easy way to get base64 encode and decode of a string
if (Object['defineProperty'] && !String.prototype.hasOwnProperty('deCode')) Object.defineProperty(String.prototype, 'deCode', { value: function() { return decodeURIComponent(unescape(window.atob(this))); } });
if (Object['defineProperty'] && !String.prototype.hasOwnProperty('enCode')) Object.defineProperty(String.prototype, 'enCode', { value: function() { return window.btoa(escape(encodeURIComponent(this))); } });
var a = 'GitHub'.enCode(); // a = "R2l0SHVi"
var b = a.deCode(); // b = "GitHub"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment