Reduce spam and junk email by using JavaScript to hide your address from spam-bots, a step-by-step tutorial.
Your web site might be contributing in how much junk mail you receive, especially if you include an email address on your pages.
Spam-lists are generated by Spam-Crawlers and Spam-Bots. These computers only purpose is to constantly comb the web in search of email addresses to add to their lists. Thankfully they are in such a rush to keep up with the massive amount of web content that they ignore everything but the plain text of each site. Executing JavaScripts would be a waste of processor cycles, but that efficiency will help to protect our email.
The Goods
The text address this script produces will appear to be plain text. From a design point of view, this will completely blend in with whatever other text you have on the page. Readers will never know it was generated by a script. Readers are free to change the font or size to their preference, and the address could be read aloud by a machine for the visually impaired.
Inserting Text with JavaScript
JavaScript's document.write() command is incredibly powerful. This function tells a user's browser to insert text or HTML into the page as it displays. That text can be used do just about anything without any visible distinction from the actual HTML or text saved on the server.
For example, document.write(), can build my email address into regular HTML text like this: joe@joemaller.com Even though you can see the email address, it was rendered by your browser when the page was displayed and is therefore safe from Spam-Crawlers. I use this trick everywhere you see an email address on this page, in fact every time you see an email address on my site a little script put it there.
The code to write an email address into a web page looks like this:
The parentheses contain the two halves of my email address. These strings are assembled by the document.write() function and output into the page.
The TYPE="text/javascript" in the script tag is necessary to bring the source code up to standards. The script will work without it, but I like tory and be as standards-compliant as I can.
Linking the address
The document.write() function can insert a whole link too. When combined with a variable, the whole script can be small, clean and easily moved.
First, we'll define a reusable variable emailE to store the email address in the same string format used earlier.
emailE=('yourname@' + 'emailserver.com')
As mentioned on my JavaScript Rollover page, variables are case sensitive, and CODEemailE is different from Emaile.
Next, we'll use document.write() to insert the link, combining strings with the variable we just stored to build a compact and protected email link.
document.write(
''
+ emailE + ''
)
Be careful with those single and double quote marks.
No JavaScript Means No Email
Since this script completely relies upon JavaScript, users without JavaScript or those who've disabled it will not see anything. Parts of Dale's image solution (mentioned above) can be made to work here using a