Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple spam protection for email addresses using jQuery
/* Simple spam protection for email addresses using jQuery.
* Well, the protection isn’t jQuery-based, but you get the idea.
* This snippet allows you to slightly ‘obfuscate’ email addresses to make it harder for spambots to harvest them, while still offering a readable address to your visitors.
* E.g.
* <a href="mailto:foo(at)example(dot)com">foo at example dot com</a>
* →
* <a href=""></a>
$(function() {
$('a[href^="mailto:"]').each(function() {
this.href = this.href.replace('(at)', '@').replace(/\(dot\)/g, '.');
// Remove this line if you don't want to set the email address as link text:
this.innerHTML = this.href.replace('mailto:', '');

This comment has been minimized.

Copy link

@zanderwar zanderwar commented Oct 13, 2019

Thanks for this.. I wanted to obscure it from scrapers a bit further as most of them are looking for mailto::

<a href="javascript:" data-email="sales[at]alto[dot]com[dot]au"></a>
$('a[data-email]').each(function () {
      this.href = 'mailto:' + $(this).attr('data-email').replace('[at]', '@').replace(/\[dot]/g, '.');
      this.innerHTML = this.href.replace('mailto:', '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment