Go to -> C and S Design ->
Articles
->
Coding
->
Hide Email
->
Javascript
-> Show Code
Click here to view or download the code for this example
Click here to view or download the code for this example
Design
Hiding email address details
These script are designed to be in an external .js file. The domain (hostname) is hardcoded into the functions. By using an external script file it means less bytes in each page to be downloaded, as the .js file is downloaded once and then cached by the browser. The same functions are then available across the site and can be called from any page it is included in.
An addition to the downloadable script file is a function to extract the hostname for the site rather than having it hardcoded. This makes the script portable between different hosts and no editing should be required to set the domain name part of the address.
This hostname extract script is explained seperately.
An addition to the downloadable script file is a function to extract the hostname for the site rather than having it hardcoded. This makes the script portable between different hosts and no editing should be required to set the domain name part of the address.
This hostname extract script is explained seperately.
First is a simple function that will just write a basic email address to the page. The recipient is sent as a parameter.
// JavaScript for writing email addresses.
function HideEmail(user)
// write email address on page to hide from 'spiders'
// recipient name passed as user.
// change domain name in this script before using on your site
{
domain = "candsdesign.co.uk";
document.write('<a href=\"mailto:');
document.write(user);
document.write('@');
document.write(domain);
document.write('\">');
document.write(user);
document.write('@');
document.write(domain);
document.write('</a>');
}
Email us at <script type="text/javascript">HideEmail("webmaster")</script>
Email us at
Basic Function to write the email address
// JavaScript for writing email addresses.
function HideEmail(user)
// write email address on page to hide from 'spiders'
// recipient name passed as user.
// change domain name in this script before using on your site
{
domain = "candsdesign.co.uk";
document.write('<a href=\"mailto:');
document.write(user);
document.write('@');
document.write(domain);
document.write('\">');
document.write(user);
document.write('@');
document.write(domain);
document.write('</a>');
}
On Page Use
Email us at <script type="text/javascript">HideEmail("webmaster")</script>
Script Output
Email us at
This second function has the user name sent as a parameter but now has the addition of a "Friendly Name" also passed as a parameter.
// JavaScript for writing email addresses.
function EmailName(user,PromptText)
// write email address on page to hide from 'spiders'
// recipient name passed as user. Friendly name passed as PromptText
// change domain name in this script before using on your site
{
domain = "candsdesign.co.uk";
document.write('<a href=\"mailto:');
document.write(user);
document.write('@');
document.write(domain);
document.write('\">');
document.write(PromptText);
document.write('</a>');
}
Email the <script type="text/javascript">EmailName("webmaster","Webmaster")</script>
Email the
Function to write the email address and a Friendly Name
// JavaScript for writing email addresses.
function EmailName(user,PromptText)
// write email address on page to hide from 'spiders'
// recipient name passed as user. Friendly name passed as PromptText
// change domain name in this script before using on your site
{
domain = "candsdesign.co.uk";
document.write('<a href=\"mailto:');
document.write(user);
document.write('@');
document.write(domain);
document.write('\">');
document.write(PromptText);
document.write('</a>');
}
On Page Use
Email the <script type="text/javascript">EmailName("webmaster","Webmaster")</script>
Script Output
Email the
This third function adds what is really a redundant layer of obfuscation to the address, by adding a second function that writes some sections of the address in unicode characters.
// JavaScript for writing email addresses.
function CodeEmail(user,PromptText)
{
domain = "candsdesign.co.uk"
document.write('<a href=\"mailto:')
WriteUnicode(user)
document.write('@')
WriteUnicode(domain)
document.write('\">')
WriteUnicode(PromptText)
document.write('</a>')
}
function WriteUnicode(varString)
{
for (i=0; i<varString.length; i++)
document.write("" + varString.charCodeAt(i) + ";")
}
Email the <script type="text/javascript">CodeEmail("webmaster","Webmaster")</script>
Email the
This script has been "broken" to show the code output
// JavaScript for writing email addresses.
function CodeEmail(user,PromptText)
{
domain = "candsdesign.co.uk"
document.write('<a href=\"mailto:')
WriteUnicode(user)
document.write('@')
WriteUnicode(domain)
document.write('\">')
WriteUnicode(PromptText)
document.write('</a>')
}
function WriteUnicode(varString)
{
for (i=0; i<varString.length; i++)
document.write("" + varString.charCodeAt(i) + ";")
}
On Page Use
Email the <script type="text/javascript">CodeEmail("webmaster","Webmaster")</script>
Script Output
Email the
Script Output in code
This script has been "broken" to show the code output

