Javascript Mailto

Javascript Mailto

The javascript mailto is a very useful tool for combating spam, which is an elegant use of this very handy scripting language.

Why Do You Need a Javascript Mailto?

At first glance, there may not seem to be a need for a javascript version of the mailto function. After all, there is a method in HTML that will automatically launch a mail client:

<A href=mailto: your.email@address.com>eMail Me!</a>

Unfortunately, spammers were able to develop "bots" that "spider" the web - that is, go through all the HTML code they can in order to find email addresses. When they find one, it goes onto a list that gets sent spam and also worse forms of junk email.

However, using a javascript mailto can defeat this tactic. The same code that tells non-javascript enabled browsers to ignore the javascript - namely, the !-- and the --! tags - keep the spiders from recognizing the email addresses.

A Sample Code Snippet

The technique for a javascript mailto is so easy that there are many different ways to create it. What follows is simply an example, modified from another example from DesignPlace. You can find many other code examples online and certainly learn more javascript to create your own.

// This script goes where you want the email link:

<script language="Javascript" type="text/javascript">

<!-- // This hides the javascript from non-enabled browsers

//The script uses the following variables

var emailPrefix = "Email Username"; //the first part of the email address
var emailDomain = "Email Domain"; //the second part -"something.com"
var subjectLine = "What is the email about?"; //Goes in subject of email
var emailText = "Write your email here"; //this goes in the body of the email
//output
document.write('<a href=\"mailto:' + user + '@' + domain + '?subject=' + subject + '&body=' + bodytext + '\">');
document.write('this is the email link</a>');
// -->

This may seem like a lot of effort to go through for one email link - and it's true, it is, or it would be except for the magic of cut and paste. More importantly, though, it protects email addresses from being scooped up.

Another Method

Of course, you can also use an online code generator like the one at MailtoProtector.com to create your mailto script. However, that site only works with Internet Explorer 5.5. or above. By entering in the pertinent information in each field on their site form, you end up with a perfectly serviceable and eminently customized javascript mailto. You can also download their software for a small fee which will generate the code for you.

A Quick and Dirty Way to Fool Spambots

The code above is great for a robust mail link - complete with subjects, body text, etc. But the main focus of it is that it breaks up the email address, which fools the spambots.

On the coding website December14.net, there is a similar method used in a much more elegant code snippet:

Click to send an <a href="mail.html" onmouseover="this.href='mai' + 'lto:' + 'name' + '@' + 'thisDomain.com'">e-mail message</a>

The amazing thing about this solution is that it uses the javascript command "onmouseover" to turn all of the small bits of letters into one recognizable email address and command, mailto: name@thisDomain.com. What's even better is that this address will show up on javascript enabled status bars as well.

The disadvantage to any of these methods, of course, is that javascript-disabled browsers will not be able to see anything, and it will make any email links dysfunctional. But if javascript is a necessary part of your site, then you should use this along with the other code elements to increase the security of your web page.

Javascript Mailto