JavaScript Programming

Email Obfuscation

February 24, 2009

As we all know spam is a plague that has spread to every far corner of the Internet.  Spam-bots collect our email addresses in many ways, but in my opinion the most likely way they’ll get your email address is from scraping webpages such as forums, where you email may be listed.  So when you’re building your site and you want to put an “email me” link you should be very careful about doing so.  Once the bots find it they’ll start sending you endless offers to buy Viagara an Cialis.

So what can you do to stop this?  There are many solutions!  But today I want to offer you a simple JavaScript file that will encrypt your all email addresses on your page automatically.  It can be downloaded from Voynex, however they don’t really offer a good explanation on how to use it.  So here is a step by step:

  1. Download the Email Guard script from Voynex’s site
  2. Extract the files and upload xmailguard.js to your site, I usually have a directory called “scripts” in the root of each site
  3. In any page you want to have your emails encrypt link the script by adding this line to the top: <script language=”javascript” type=”text/javascript” src=”/scripts/xmailguard.js”></script>
  4. Initialize the script by adding another script block : <script language=”javascript” type=”text/javascript”>xmgInit()</script>
  5. Now you have to type your email links a bit differently, instead of <a href=””></a> you will have to code your links to <a href=””></a> and when the page loads it will rewrite them for you.   If you are using PHP this line will do most of the work for you: $email = str_replace(“@”, “_at_”, $email);
  6. Now its possible that some bots will render the javascript first before doing their dirty work, but from my experience I have found they don’t process the javascript.  You can also customize the encrypted syntax you want to use by tweaking the first couple of lines in xmailguard.js

Now your email addresses are safe!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.