Over a million developers have joined DZone.

Hide Email Address From Spammers With Javascript

·

function mangle() {
	if (!document.getElementsByTagName && !document.createElement &&
		!document.createTextNode) return;
	var nodes = document.getElementsByTagName("span");
	for(var i=nodes.length-1;i>=0;i--) {
		if (nodes[i].className=="change") {
			var at = / at /;
			var dot = / dot /g;
			var node = document.createElement("a");
			var address = nodes[i].firstChild.nodeValue;

			address = address.replace(at, "@");
			address = address.replace(dot, ".");

			address = address.replace(at, "@");
			address = address.replace(dot, ".");
			node.setAttribute("href", "mailto:"+address);
			node.appendChild(document.createTextNode(address));
			
			var prnt = nodes[i].parentNode;
			for(var j=0;j

You can use this script, if you'd like to post your email address to your homepage, but don't want to see it picked up by spammers.

Just run it in your onload event handler and mark emails in your HTML as:

      markos at gaivo dot net

This way they are still recognizable by those who don't use javascript.

If you need help or want to discuss this code, please do it on my blog.
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}