HTML Window atob( ) Method


The HTML window btoa() method encodes a string of data in base-64 string which can be decoded by atob() method in the HTML document.

Syntax

Following is the syntax −

window.btoa(string_to_be_encoded);

Let us see an example of HTML window btoa() method −

Example

 Live Demo

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
   ::placeholder {
      color: #000;
   }
</style>
<body>
<h1>HTML Window atob()/btoa() Method</h1>
<textarea placeholder="Enter your message here" cols="30" rows="5"></textarea>
<button onclick="encode()" class="btn">Encode Message</button>
<button onclick="decode()" class="btn">Decode Message</button>
<div class="show"></div>
<script>
   var show = document.querySelector(".show");
   function encode() {
      var encMsg = window.btoa(document.querySelector("textarea").value);
      show.innerHTML = encMsg;
   }
   function decode() {
      var decMsg = window.atob(window.btoa(document.querySelector("textarea").value));
      show.innerHTML = decMsg;
   }
</script>
</body>
</html>

Output

Enter a message in the white text area and then click on “Encode Message” button to show encoded message using btoa() method.

Then click on “Decode Message” button to display decoded message using atob() method.

Updated on: 26-Sep-2019

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements