Codelife Codelife - 2 months ago 10x
HTML Question

Place the encoded Javascript in HTML page

This is a HTML page which contains a script. This script in the HTML page is plain text. I want to know how to place this script with base64 encoding in html page. HTML file :-

<!DOCTYPE html>


<p id="demo"></p>

var pi = 3.14;
document.getElementById("demo").innerHTML = pi;


Script is :-

var pi = 3.14;
document.getElementById("demo").innerHTML = pi;

Base64 encoding of script :-



Doing this will

  1. increase the size of your script, since base64 inflates data size
  2. provide virtually zero security benefit whatsoever (or any obvious benefit, for that matter), since users can easily decode the script just as easily as you encoded it

In case you are interested in making your script less-efficiently encoded for no obvious benefit, you can do so by base64-encoding the script (not including the <script> tags) and include it as a data: URL:

<script src="data:text/javascript;base64,dmFyIHBpID0gMy4xNDtkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgiZGVtbyIpLmlubmVySFRNTCA9IHBpOw=="></script>

This doesn't really provide any obfuscation benefit, as pasting the data:text/javascript;base64,dmFyIHBp... URL into your address bar shows the decoded text.

Alternatively, you could pass it into atob to decode it and then pass it into eval:


Again, this affords no security benefit, since any user can easily run this code with the eval and see the decoded text.