C and S Design.
Search Friendly Programming and Design

Go to -> C and S Design -> Articles -> Coding -> Hide Email -> ASP Hide Email -> Show Code

Click here to view or download the code for this example


Design

Hiding email address details

Using Server Side ASP

The simplest way to check for non-browsers user agents is to test for a session variable when the page is first requested if it is not there then set the session value, If it is there set a variable on the page to true for later checks in the code. This code also sets some constants for an email address and contact form pagename and should be put above the document <head>. The simplest method would be for this code to be in an separate file and included into the page.
Code to check or set session variable

<%
' session test for crawlers or non-browser user agents
' code in top of page
const strDefMailAddress = "webmaster@domain.com"
const strContactForm = "/contact-us.asp"

dim bBrowserCheck
if session("browsercheck") then
bBrowserCheck = session("browsercheck")
else
session("browsercheck") = true
bBrowserCheck = false
end if
%>

It is important to test first and set to true if false rather than setting first then checking. This is simply because if the session variable is set to true it will be always true on the same page. The reason being is that the variable will exist in the memory space allocated to the page instance regardless of the cookies status of the user agent. By checking first the test is actually for if the state is persistent between pages. The test will always return false on the first access to the page, but if the user agent then visits another page and the test will be true for UA's accepting cookies but still false for non-acceptance.
This basic function simply a series of response.write instructions to output the HTML to the user agent. One parameter is needed, that of the email address of the recipient. If an empty string is passed a pre-configured address is used.
The function code should be added to either a global function include or to the same include as the session checking code.
Function to Write Contact Details

<%
' code to display links
function WriteContact(strMailAddress)
if strMailAddress = "" then strMailAddress = strDefMailAddress
with response
.write "<a href="
.write chr(34)
if bBrowserCheck then
.write "mailto:"
.write strMailAddress
.write chr(34)
.write " title="
.write chr(34)
.write "Contact us via email"
else
.write strContactForm
.write chr(34)
.write " title="
.write chr(34)
.write "Contact us via a form"
end if
.write chr(34)
.write ">"
.write "Contact Us"
.write "</a>"
end function
%>


This function above can be easily extended to use multiple email addresses that can be also transfered across to the contact form without exposing the addresses to web crawlers. Which will be covered in a later article.

Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design