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 !
Matt
January 7, 2008, January 07, 2008 16:09, permalink
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>'); }
jamesgolick
January 8, 2008, January 08, 2008 04:34, permalink
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>'); }
shtruc
January 8, 2008, January 08, 2008 13:12, permalink
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' + '@' + 'domain' + '.com' + '" >' + 'myemail' + '@' + 'domain' + '.com' + '</a>'); }
James Seigel
January 11, 2008, January 11, 2008 15:10, permalink
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>'); }
Rob
March 10, 2009, March 10, 2009 17:59, permalink
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>
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.