Polity Research Ltd
IT { innovation; analysis; design; development; review }

How to hide mailto links from email address harvesters

Problem

You wish to provide simple mailto: links on your site for visitors to use, but do not want those addresses to be harvested for spam lists.

Solution

Add some javascript like the following to your site's scripts. Whenever a page loads, this onload handler will scan the document and convert links written in you own obfuscated format into regular mailto links. Once transformed by the script, the links work normally and your visitor will never notice the difference.
/* regular expression for the obfuscated href format */
var contactPattern = /^contact\:(.*)\#(.*)$/;

/* regular expression for the obfuscated inner html format */
/* if it is also a representation of the mailbox */
var displayPattern = /^(.*)\#(.*)$/;

function deMungeMailtos() {
  for (var i=0; i<document.links.length; i++) {
    var a = document.links[i];
    var m = a.href!=null ? contactPattern.exec(a.href) : null;
    if (m!=null) {
      a.href = 'mailto:'+m[1]+'@'+m[2];
    }
    m = displayPattern.exec(a.innerHTML);
    if (m!=null) {
      a.innerHTML = m[1]+'@'+m[2];
    }
  }
}

/* run deMungeMailtos on load */
if (window.addEventListener) {
  window.addEventListener('load', deMungeMailtos, false); 
} else if (window.attachEvent) {
  window.attachEvent('onload', deMungeMailtos);
} else {
  window.onload = deMungeMailtos;
}
In this case the obfuscated format involves using contact instead of mailto as the link protocol, and a # instead an @ to separate the localpart from the domain part of the mailbox.

For example:

<a href="contact:nospam#invalid?subject=test">nospam#invalid<a>
when loaded becomes:
<a href="mailto:nospam@invalid?subject=test">nospam@invalid<a>
A downside to this approach is that non-Javascript browsers will see the obfuscated form, which they will not be able to interpret meaningfully.

If this is an issue, you could change contactPattern to be the url of a page with a web-based submission form on it.

For example if you have a contact page called mailform.php that takes separate localpart and domain parameters, you could use the following:

/* regular expression for the obfuscated href format */
var contactPattern = /^mailform.php\?localpart=\:(.*)\&domain=(.*)$/;
Hope this helps!

Boookmark this using:  Del.icio.us  |  Digg  |  Technorati  |  Blinklist  |  Furl  |  reddit
Copyright © 2003-2008 Polity Research Ltd. All Rights Reserved.