JavaScript: Masking eMail Addresses

Spammers are evil people with crafty tools. Their nefarious 'bots are constantly spidering the web, looking for injudiciously displayed email addresses. Post your email address on a board on one day, you can count on 200 offers for cut rate "\/!agra" the next. What is the conscientious web master to do?!?

Well, the key is to understand the limitations of the nefarious 'bots. You see, they can only parse the strings that are actually written on your web page. This is key because you can display an email address without actually putting it explicitely on your web page. Consider this snippet:

function writemail(emailtld, emaildomain, emailacct){
  document.mymail = emailacct + '@' + emaildomain + '.' + emailtld;
  document.write("<a href=\"mailto:" + document.mymail + "\">");
  document.write(document.mymail + "</a>;");
}

So, given a top level domain (e.g. "net"), a domain (e.g. "ziemecki"), and an account (e.g. "nosuch"), this function will first reconstruct the email address into the standard format, and assign it to a variable called "document.mymail". The two "document.write" calls then will output the eMail address sandwiched within an HTML "mailto" string. To call it, you would put something like this on your web page:

For problems or questions regarding this web site send email to
<script language="JavaScript">sendmail("net","ziemecki","nosuch")</script>

What the site reader will see is something like this:

For problems or questions regarding this web site send email to nosuch@ziemecki.net

As you can see, the elements of the email address are not arranged on the web page in any format remotely resembling a normal address. However, since the JavaScript is visually rendered on page load, the visual and functional attributes of what is displayed are that of a normal mailto link from the viewpoint of the reader. Since a 'bot can't see how the browser has rendered the JavaScript, it will ignore the address. It is, effectively, obfuscated to the 'bot and clear to the human.

Doesn't fighting spam make you feel good inside?

Tags: