/

HTML / CSS / JavaScript Tutorial

JavaScript Window method: btoa()

[this page | pdf | back links]

The btoa() method (when applied to Window objects in the JavaScript BOM) encodes a string into base-64, using A-Z, a-z, 0-9, “+”, “/” and “=” characters to encode the string. The string can be decoded using the atob() method.

 

It has the following syntax with the following parameters. It returns the encoded string.

 

window.btoa(str)

 

Parameter

Required / Optional

Description

str

Required

String to be base-64 encoded

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,td,tr,caption {border: thin solid black; border-collapse: collapse;}
</style>
</head>
<body>
base-64 encoding and decoding:<br><br>
<table>
<tr><td><span id="x1"></span></td>
  <td>encodes to</td><td><span id="y1"></span></td></tr>
<tr><td><span id="x2"></span></td>
  <td>decodes to</td><td><span id="y2"></span></td></tr>
</table>

<script>
var x1 = "?qr";
var y1 = window.btoa(x1);
var x2 = y1;
var y2 = window.atob(x2);
document.getElementById("x1").innerHTML = x1;
document.getElementById("y1").innerHTML = y1;
document.getElementById("x2").innerHTML = x2;
document.getElementById("y2").innerHTML = y2;
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodWindowBtoa() {
  return !!window.btoa;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile