Published by Rob Scaife on 26 Aug 2008
Protecting your Website from Spammers
NOTE: As an object lesson, the webmaster address for my old Rotary domain was subjected to a flood of crude solicitations before I implemented this technique. With spam levels increasing dramatically year over year, using a script such as the one below to protect your email addresses has gone from nice to necessary. Take the time to learn to use the scripts below.
SPAM is a term for unsolicited commercial email messages. Commercial spam is big business, and its purveyors use automated software called spambots to trawl websites, newsgroups and chat rooms looking for fresh email addresses to add to their lists. Savvy emailers know to limit spam in their inboxes by never giving their email address in newsgroups or chatrooms and using a throwaway address from services like yahoo.com when they need to provide an email address in an unfamiliar environment.
But how can you prevent email addresses being harvested from your website? If you are willing to learn a little JavaScript, it’s easily accomplished.
The technique shown in Sample 1 below has kept my club website almost entirely spam-free for several years.
(The colours in the samples below are purely for clarity on this page, they will be different in whatever editor you use for your pages. The red text shows how the email address fits into the code.)
A normal email address link coded in a page looks like the following (on one line):
<A HREF=”mailto:president@rotary.ca“>president@rotary.ca</A>
Sample 1
That code will appear on the web page as president@rotary.ca. You can see that the email address appears in two different places in the code and once on the rendered page, making it quite easy for a spambot to pick it up.
The trick I’m going to offer you here is a JavaScript technique that will make it difficult or impossible for the spambot to recognise the pattern of an email address in your webpage.
Sample 2 shows the same entry set up with JavaScript to obscure the address from spambots. The last six lines are the JavaScript code. the lines starting with “document” and “+” would usually appear on the same line, it’s just compressed here to fit the display.
Note also that the address as it will be displayed in the page will have a space between each character. This is easy enough to read, but obscures the address from spambots.
<script language=”javascript”>
<!–
document.write(”<a href=” + “mailto:president”
+ “@rotary.ca” + “>” + “president @ rotary.ca” + “</a>”)
//–>
</script>
Sample 2
Notice how the email addresses are broken up in sample 2.
If you place the text in sample 2 above in the HTML code of your web page or newsletter entry, replacing the black and red text with the appropriate information, this will work for you.