|
Display email but avoid spam
Introduction
This guide will demonstrate how to display an email link on
your webpage while avoiding the risk of your address being
harvested by spammers robots. It's just basic javascript and very
easy to customise.
Creating the Javascript.
The first step is to define the variables. The following goes
into the <HEAD> of your document:
<script language="JavaScript"
type="text/javascript">
var name = "webmaster";
var address = "domain.com";
var link = "Mail me!";
var subject = "Feedback";
function protectmail() {
document.write('<a class="email" href=mailto:' + name + '@' +
address + '?subject=' + subject + '>' + link +
'</a>');
}
</script>
To walk throught this (if you are not familiar with
javascript): first we define the variables (var) we need
to make up the link. Each variable has a value, the name variable
(var name) is webmaster, the address variable is your
domain, domain.com, the link variable is the link which will be
displayed on screen (Mail me!), the subject variable is the
subject of the email. You can add or take away variables as you
need to, so long as they are reflected in the function...
The function basically ties all the variables together
to create the email link. "document.write" tells the browser to
"write" the following; <a class="email" href=mailto:
(basic mailto link code) + the name variable + the @ symbol + the
address variable + ?subject= (code to add a subject to the
mail link) + the subject variable then close the tag with
< then write the link variable (Mail me!) and then
close the link tag with </a>.
Which equals:
<a class="email" href=mailto:' + webmaster +
'@' + domain.com + '?subject=' + Feedback + '>' + Mail me! +
'</a>
Defining the variables and function above is not enough. Now
we need to tell the browser where to "write" this link with the
following. Remember, the function is protectmail() so
where ever you want your email link to appear simple add the
following:
<script language="JavaScript"
type="text/javascript">
protectmail();
</script>
The result is this is our mail link:
Full Code
Into your document head:
<script language="JavaScript"
type="text/javascript">
var name = "webmaster";
var address = "domain.com";
var link = "Mail me!";
var subject = "Feedback";
function protectmail() {
document.write('<a class="email" href=mailto:' + name + '@' +
address + '?subject=' + subject + '>' + link +
'</a>');
}
</script>
Into your document body:
<script language="JavaScript"
type="text/javascript">
protectmail();
</script>
|