Projects‎ > ‎

mailto:decode

about…

Most people realise now that including ones email address on a web page is an invitation for spam. It is common practice to obfuscate email addresses with a nospam infix, simple character replacement or some other method.

Here I present some simple JavaScript I wrote to dynamically de-obfuscate every <a href="mailto:user(at)example(dot)com">mailto:-type</a> anchor tag on the page. Add it to your window.onload handler, and let it work its magic.

code…

function decodeString(z)
{
   var code, symbol;
   code = { '': /\s+/,
           '@': /\(at\)/i,
           '.': /\(dot\)/gi };

   for (symbol in code)
      z = z.replace(code[symbol], symbol);

   return z;
}

function decodeLink(link, recurse)
{
   if (!link.getAttribute || !link.hasChildNodes
         || !(link.nodeType && link.nodeType == 1))
      return;

   var href = link.getAttribute('href');

   if (!href || !href.match(/^mailto:/))
      return;

   link.setAttribute('href', decodeString(href));

   if (recurse
      && link.hasChildNodes() && link.firstChild.nodeType == 3)
   {
      var text = link.firstChild.nodeValue;
      link.firstChild.nodeValue = decodeString(text);
   }
}

function decodeMailtos()
{
   if (!document.getElementsByTagName)
      return;

   var links = document.getElementsByTagName('a');

   for (var i = 0; i < links.length; ++i)
      decodeLink(links[i], 1);
}

window.onload = decodeMailtos;
Comments