Hiding from Email Harvesters





12
Date Submitted Tue. Oct. 3rd, 2006 5:51 AM
Revision 1 of 1
Helper Thomas
Tags Email | JavaScript | spam
Comments 1 comments
Here's a simple JavaScript solution to hide your e-mail from many spam scripts while still providing clickable hyperlinks to your visitors.

Web crawlers and visitors with JavaScript disabled will see: me [at] mydomain [dot] com. I've seen a lot of people that just leave it at this, but it seems a bit unprofessional in my opinion. With the following code, we can replace that with a fully functional hyperlink.
<span class="encoded-email">me [at] mydomain [dot] com</a>
function decodeEmails () {
        if (document.getElementsByTagName) {
                var spans = document.getElementsByTagName("span");
               
                for (var n = 0; n < spans.length; n++) {
                        if (spans[n].className == "encoded-email") {
                                var email = spans[n].firstChild.nodeValue.replace(" [at] ", "@");
                                email = email.replace(" [dot] ", ".");
                               
                                var hyperlink = document.createElement("a");
                                var linkText = document.createTextNode(email);
                                hyperlink.href = "mailto:" + email;
                                hyperlink.title = "Send me an e-mail";
                                hyperlink.appendChild(linkText);
                               
                                spans[n].replaceChild(hyperlink, spans[n].firstChild);
                               
                                linkText = null;
                                hyperlink = null;
                        }
                }
        }
}
Just call the decodeEmails() function using your favorite window load event handler.

Thomas Higginbotham

thomashigginbotham.com/

Comments

Comments Revision
Tue. Oct. 3rd, 2006 5:59 AM    Helper Thomas

Voting