Encode a string in base-64:
Click the button to encode a string in base-64.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w w w.j a v a2s .c o m*/ var str = "Hello World!"; var enc = window.btoa(str); var res = "Encoded String: " + enc; document.getElementById("demo").innerHTML = "The original string: " + str + "<br>" + res; } </script> </body> </html>
The btoa()
method encodes a string in base-64.
This method uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode the string.
Use the atob()
method to decode a base-64 encoded string.
btoa(str);
Parameter Values
Parameter | Description |
---|---|
str | Required. The string to be encoded |
The btoa()
method returns a String representing the base-64 encoded string.