The btoa()
method encodes a string in base-64.
btoa |
Yes | Yes | Yes | Yes | Yes |
window.btoa(str)
Parameter | Description |
---|---|
str | Required. The string to be encoded |
The base-64 encoded string
The following code shows how to Encode a string in base-64.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- w w w .j av a 2 s. c om-->
<script>
function myFunction() {
var str = "Hello World!";
var enc = window.btoa(str);
var res = "Encoded String: " + enc;
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
The code above is rendered as follows: