<script type="text/javascript"> <!-- This script hides the email address from spiders -->
<!-- Begin hide from old browsers
username = "webmaster";
site = "ibcboston.org";
document.write('<a href="mailto:' + username + '@' + site + '">');
document.write(username + '@' + site + '</a>');
// End -->
</script><noscript><div>webmaster _at_ ilfilosofo _dot_ org</div></noscript>
<span class="hiddenemail"> <span class="name">webmaster</span> _at_ <span class="domain">ilfilosofo</span> _dot_ <span class="tld">com</span> </span>And run the function below when the page loads (e.g. window.onload = revealEmails();)
//************************************************************************
function revealEmails() { //changes hidden email names within span tags to clickable addresses
//looks for span class of 'hiddenemail'
// inside that class it looks for span classes of 'name','domain', and 'tld'
// from https://austinmatzko.com/blog/2005/06/28/javascript-email-hiding/
//************************************************************************
var x = document.getElementsByTagName('span');
for (var i=0;i<x.length;i++) {
var address;
if (x[i].className == 'hiddenemail') {
var y = x[i].getElementsByTagName('span');
for (var j=0;j<y.length;j++) {
if (y[j].className == 'name') {
if (y[j].innerHTML !== null)
address = y[j].innerHTML + '@';
}
if (y[j].className == 'domain') {
if (y[j].innerHTML !== null)
address = address + y[j].innerHTML + '.';
}
if (y[j].className == 'tld') {
if (y[j].innerHTML !== null)
address = address + y[j].innerHTML;
}
}
if ((x[i].innerHTML !== null) && (address !== null))
x[i].innerHTML = '<a href='mailto:' + address + ''>' + address + '</a>';
}
}
}
One Comment
Why not simply use a different email address format inside the hrefs of links and use JS to rewrite into a conventional form?
The author has little to do other than to reformulate email addresses and the page structure and styling is not impacted at all.
Instead of … href=”mailto:name@domain.tld” … write href=”email:domain.tld!name” and the following suffices to make the necessary transformations (it also transforms any occurreneces of the mangled email address in the link innerHTML) :
for (var m, x = document.getElementsByTagName ("a"), i = x.length; i--;)
if (m = x[i].href.match (/email:(.+)!(.+)/i)) {
x[i].href = 'mailto:' + m[2] + '@' + m[1];
x[i].innerHTML = x[i].innerHTML.replace (m[1] + "!" + m[2], m[2] + '@' + m[1]);
}
One Trackback
[…] Over a year ago I showed how I like to hide email addresses using JavaScript. A recent project made it convenient to wrap that into a WordPress plugin. […]