JavaScript

Hide your email address from spam-bots!

Can you tell the difference between Spam Email and Ham Email? - See our article on beating the spammers!

We all want to be contacted but at the same time we all hate junk email.  How do they get your email address? Well, one way is that they write spam-bots that search the web for email addresses, and each new one found is added to the list.

How can you put your email address on a web page but prevent the spam-bots from seeing it?  One way is to use JavaScript to write your email address to the page.  In this article, we'll look at how you can use document.write() to do just that!

Shields-up against spam-bots!

Using JavaScript to write your email address to a page is easy.  Here's one way to do it:

<script type="text/javascript">
    document.write('fred@someaddress.com')
</script>

Cool script, but unfortunately it doesn't help you because the HTML of the page still contains a full email address logically formatted.  The spam-bot will lock on to the @ and take everything before it until it come to a space and everything after it until it come to a space, thus getting the whole email address.

Notice I said "logically formatted" - that's the key with spam-bots.  Because they are automated they look for logical formatted email address.  They start by looking for the @ symbol and work from there.  The key is to break up the email address in the HTML of the page so that it is no longer logically formatted but when loaded into the page, is.  Here is one way you could do this:

<script type="text/javascript">
<!-- 
    document.write('fred@' + 'someaddress.com')
//-->
</script>

The logical format of the address has been broken up.  Want to be a bit more sophisticated, try this:

<script type="text/javascript">
<!--    
   
document.write('fr' + 'ed@' + 'someaddr' + 'ess.com')
//-->
</script>

Much better ... even the domain name is broken up so the spam-bot will miss the someaddress.com too!

Notice how if you put that script in a page, the email address will show but not as a hyperlink (because the a tag is not present).  Want ti to actually appear on a page as a link?  No problem!  Here's one method that does it all!

<script type="text/javascript">
<!-- 
    address2='ress.com'

    address1='someadd'
    address=('fred' + '@' + address1 + address2)
    document.write('<A href="mailto:' + address + '">' + address + '</a>')
//-->
</script>

Notice how we've used two variables to hold the two parts of the domain name and reverse them (added cleverness!), then assembled the lot at the point of writing to the page -  that should keep most bots at bay!

NOTE: If you are worried that spam-bots catch on to your idea, you can always modify it, say by breaking the email address up into 4 or 5 parts, and mixing them up in different orders even more!

For those of you worried about visitors not using a JavaScript enabled browser (shame on them!), you could always add the following note to the page that will show up for those not JavaScript enabled:

<script type="text/javascript">
<!-- 
    address2='ress.com'
    address1='someadd'
    address=('fred' + '@' + address1 + address2)
    document.write('<A href="mailto:' + address + '">' + address + '</a>')
//-->
</script>
<noscript>
    <em>Email address is JavaScript protected.<br>
    Please enable JavaScript if you want to contact me.</em>
</noscript>

Enjoy dodging spam-bots!



Kathie Kingsley-Hughes
Last updated: May 4th 2004
Print This Page   |   Email me when this page changes    |  Search This Site



Crucial.com System Scanner does the work for you!



udes/a_global/close_links.inc"); ?>

Contact Us