How To Make Javascript E-Mail Links

One useful yet widely unused method of preventing e-mail spam is to use Javascript. Many people post regular mailto: links which can be used by spam bots to snatch up your e-mail address.

Here is a quick example of a basic Javascript E-Mail Link:

<a href="#" onClick="window.location='mailto:email@example.com';">E-Mail Me</a>

But there is a big problem with that… it still displays your e-mail address. Spam bots will read in the content of your site and when it sees text in the format of an e-mail addy it will read it in. So why not split things up a bit?

<a href="#" onClick="window.location='mailto:email' + '@' + 'example.com';">E-Mail Me</a>

I can’t guarantee that it is flawless, but since most spam bots disable Javascript you should be safe from 99% of them. Oh, and don’t forget that if your user has Javascript disabled it won’t work, which is why I prefer contact forms.

Please subscribe, or else I will cry. Do you really want to make a programmer cry?

3 Comments

  1. MacBros Says:

    This is actually better:

    <script language = "javascript" type="text/JavaScript">
    <!–
    var username = "user";
    var hostname = "yourdomain.com"
    var linktext = username + "@" + hostname;
    document.write ("<a href=" + "mail" + "to:" + username + "@" + hostname + ">" + linktext + "</a>")
    //–>
    </script>

  2. Jeremy Says:

    True, you could even use the DOM methods for adding elements.

  3. Changed Contact Page Says:

    […] just changed around the contact page and got rid of the contact form and replaced it with a Javascript e-mail link. Tags: e-mail link javascript […]

Leave a Reply

Note: By submitting your comment you agree to this blog's comment policy.

If you want a little icon next to your name - sign up for one at Gravatar.