San Tzu

Simpliest way to obfuscate e-mail address using jQuery

Here is a simpliest jQuery code to protect e-mail addresses on your website from spam bots. Just put this in a script tag into the head section:

$(window).load(function() {			
	// anti spam
	var r='random-string';
 
	$('.'+r).each(function() {
	var $this = $(this),
		value = new String($this.text());
 
		value = value.replace('['+r+']', '@');
 
		$this.replaceWith($('').text(value).attr('href', 'mailto:'+value));
	});
});

Now replace random-string with any really aleatory set of characters eg. dhhIDu338

And here is the HTML part. Publish every e-mail address on your page using this code:

Contact: <span class="random-string">johndoe[random-string]mail.com</span>

Published by

Konrad Fedorczyk

Konrad Fedorczyk

I'm interested in programming and gamedev. I especially luv HTML5 and everything connected to web technologies.

One thought on “Simpliest way to obfuscate e-mail address using jQuery”

Leave a Reply

Your email address will not be published. Required fields are marked *