How To Hide Email Address From Bots Using JavaScript

Aug 8, 2017
Base64 encoded email address

Put in the HTML link container to show your email address, where data-secret is your Base64 encoded email address. You can find plenty of websites for Base64 encoding or use JavaScript btoa function to encode your email address.

<a id="contact-link" href="#" data-secret="aGVsbG9AbXlkb21haW4uY29t">[Javascript required]</a>...<script>window.onload = function() {  var contact = document.getElementById("contact-link");  contact.textContent = atob(contact.dataset.secret);  contact.href = atob("bWFpbHRvOg==") + atob(contact.dataset.secret);};</script>

window.onload might not be the best choice to trigger the function, refer to cross browser dom ready.

The JavaScript will decode the Base64 encoding and generate the following output:

<a id="contact-link" href="mailto:[email protected]" data-secret="aGVsbG9AbXlkb21haW4uY29t">[email protected]</a>

Obfuscate email

To hide something, we need to encode it.

Base64

The easiet (but not the most secretive) method to encode/decode would be Base64 encoding using atob/btoa function which already exist in most browsers (minimum of IE10 required).

Reverse string

A common method to obfuscate email address is to reverse it. You can also replace @ with * and . with ~.

The following function will decode moc~niamodym*tset into [email protected].

<script>function decode(value) {  return value.replace('*', '@').replace(/~/g, '.').split("").reverse().join("");}</script>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.