mangleEmail()I've written a Javascript function that transparently hides your e-mail address from spambots—provided that spambots don't execute Javascript. It doesn't impact the end user in most cases. The CodeYou'll probably need to scroll horizontally in order to see long lines in their entirety. If so, a scroll bar will be at the bottom of the code block. function mangleEmail() {
if(!document.getElementsByTagName) return; //be nice to old browsers
//find out where we need to make replacements
var mangleLocations = document.getElementsByTagName("span");
var whichClass = "mangle-email"; //this class tells us where to add links
var whichClassStrike = "mangle-email-strike"; //strikethrough class
//e-mail address to mangle
var part1 = '<a href=\"mai';
var part2 = 'lto:';
var part3 = 'username';
var part4 = '@';
var part5 = 'domain.com';
var part6 = '\">';
var part7 = '</a>';
//loop through all <span>s
for(var i = 0; i < mangleLocations.length; i++) {
//find struck text
if(mangleLocations[i].className == whichClassStrike) {
mangleLocations[i].className = whichClass;
mangleLocations[i].title = "";
}
//add the link
if(mangleLocations[i].className == whichClass) {
var mailtoText = mangleLocations[i].innerHTML;
mangleLocations[i].innerHTML = part1 + part2 + part3 + part4 + part5 + part6 + mailtoText + part7;
}
}
}
Explanation (or, skip to the usage instructions)My method of explaning this function is shamelessly copied from quirksmode.org. I'll procede section-by-section, repeating the relevant section, then putting the explanation below it. function mangleEmail() {
if(!document.getElementsByTagName) return; //be nice to old browsers
To begin with, we test to find out if the browser supports the key method. If it doesn't, the function ends, sparing us from Javascript error messages. var mangleLocations = document.getElementsByTagName("span");
var whichClass = "mangle-email"; //this class tells us where to add links
var whichClassStrike = "mangle-email-strike"; //strikethrough class
//e-mail address to mangle
var part1 = '<a href=\"mai';
var part2 = 'lto:';
var part3 = 'username';
var part4 = '@';
var part5 = 'domain.com';
var part6 = '\">';
var part7 = '</a>';
Now we set up our variables. Be sure to set //loop through all <span>s
for(var i = 0; i < mangleLocations.length; i++) {
Here's where the action begins. We'll loop through our array of //find struck text
if(mangleLocations[i].className == whichClassStrike) {
mangleLocations[i].className = whichClass;
mangleLocations[i].title = "";
}
First, we check for the class that
//add the link
if(mangleLocations[i].className == whichClass) {
var mailtoText = mangleLocations[i].innerHTML;
mangleLocations[i].innerHTML = part1 + part2 + part3 + part4 + part5 + part6 + mailtoText + part7;
}
Now, we store the text contained by every How to Use this FunctionPreparationPut window.onload = init;
function init() {
mangleEmail();
//other functions to call onload go here
}
If you plan on using the strikethrough form, be sure to use some appropriate CSS code. I use: span.mangle-email-strike {
text-decoration:line-through;
cursor:help;
}
Simple FormTo use this script in its simplest form, simply enclose the text you want to be linked in Strikethrough FormThere might occasionally be a situation where the user would likely be confused by a not having a link if the function doesn't work—if the user has Javascript disabled or they're using a very old browser. For example, you might have a list of links, including using © 2005 Scott Severance |