70a3fa2b5cef1685f96ed038a63af2e4

Here's a straightforward anti-spam mailto Javavascript function so that email gathering spam robots don't see an email address on a Web page.

In my criteria for an interesting function, the mailto should display and send to the same email address. It should be easy for someone not too technical to change the email address. Here, the email address is: myemail@xyz.com.

I'm intrigued to see esoteric and clever ways people can refactor this while still keeping it easy for someone not technical to modify the email address.

1
2
3
4
5
6
7
8
function showEMail() {
   var e = [ 'm','y','e','m','a','i','l','@','x','y','z','.','c','o','m' ];
   var s = '';
   for(var i=0; i< e.length; i++) {
      s += e[i];
   }
   document.write('<a href="mailto:' + s + " >' + s + '</a>');
}

Refactorings

No refactoring yet !

Avatar

Timmeh

January 7, 2008, January 07, 2008 01:17, permalink

No rating. Login to rate!
02faa2c078a7091c214604125f91a04e

Matt

January 7, 2008, January 07, 2008 16:09, permalink

No rating. Login to rate!

might this be a little easier for someone less technical to understand how to edit?

1
2
3
4
5
function showEMail() {
   var e = [ 'myemail','xyz','com' ];
   var s = e[0] + '@' + e[1] + '.' + e[2];
   document.write('<a href="mailto:' + s + " >' + s + '</a>');
}
F6eddf2f983d23c2d031e407852625e9

jamesgolick

January 8, 2008, January 08, 2008 04:34, permalink

No rating. Login to rate!

document.write really sucks. This will work with prototype.js, but should be portable to any other js lib, or raw javascript (but why would you do that?).

Just pass in the id of the element you want updated, and this thing will do it (disclaimer: completely untested :)).

1
2
3
4
5
6
7
8
function showEMail(element)
{
   var e = { name: 'myemail',
             domain: 'somedomain',
             extension: 'com' };
   var address = [e.name, '@', e.domain, e.extension].join('');
   $(element).update('<a href="mailto:'+address'">'+address+'</a>');
}
C2eee2044eac9c9b7f7d952bd3a7c6fc

shtruc

January 8, 2008, January 08, 2008 13:12, permalink

No rating. Login to rate!

Why bother with arrays. Email hunters will have no difficulties parsing array elements or separated strings. Scrambling the array will be something interesting.

1
2
3
function showEMail() {
   document.write('<a href="mailto:' + 'myemail' + '&#64;' + 'domain' + '.com' + '" >' + 'myemail' + '&#64;' + 'domain' + '.com' + '</a>');
}
Avatar

James Seigel

January 11, 2008, January 11, 2008 15:10, permalink

No rating. Login to rate!

Simplified original removing the loop.

1
2
3
4
5
function showEMail() {
   var e = [ 'm','y','e','m','a','i','l','@','x','y','z','.','c','o','m' ];
   var s = e.join('');
   document.write('<a href="mailto:' + s + " >' + s + '</a>');
}
39baaa475e7ee1bbcc28a7f802548b89

Rob

March 10, 2009, March 10, 2009 17:59, permalink

No rating. Login to rate!

Works by adding a unique string after the @ symbol then removing it in js.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<script type="text/javascript">
function showEMail(elId, addr) {
   var addr = addr.replace('@del', '@');
   var el = document.getElementById(elId);
   el.href = 'mailto:' + addr;
   el.innerHTML = addr;
}
</script>
</head>
<body onload="showEMail('myemail', 'someone@delsomewhere.com')">
  <a id="myemail"></a>
</body>
</html>

Your refactoring





Format Copy from initial code

or Cancel