This page shows samples of how my EncodedEmail function for ColdFusion works.
You can Download the function at the bottom of the page.

This function will output a JavaScript email address that can't be harvested by SPAM Bots. I use this script to output email addresses on my site. Visitors can read the addresses, but robots that read the underlying code can't find the emails. Special thanks to Project Honeypot for the inspiration for this code. They provided the basis for the JavaScript portion of this.

The function takes up to four arguments:

  • EmailAddress - This is the email address to display. It can also contain query string parameters, like test@test.com?subject=Hello
  • CSS_Class - Optional CSS Class that can be applied to the mailto tag.
  • MailTo - If this is set to False then only the address will be output, not a mailto link.
  • LinkText - If you want alternate text for the linked text you can enter it here, i.e. <a href="mailto:test@test.com">Name</a>

 
Download EncodedEmail() function:


<!--- ============ ENCODEDEMAIL() - OUTPUT A JAVASCRIPT EMAIL ADDRESS THAT CAN'T BE HARVESTED BY SPAM BOTS ============ --->
<CFFUNCTION name="EncodedEmail" returnType="string" output="no" hint="Outputs a JavaScript email address that can't be harvested by spam bots.">
<CFARGUMENT name="EmailAddress" type="string" required="true">  <!--- This is the email address to display.  It can also contain query string parameters, like test@test.com?subject=Hello --->
<CFARGUMENT name="CSS_Class" type="string" required="false" default="">  <!--- Optional CSS Class that can be applied to the mailto tag. --->
<CFARGUMENT name="MailTo" type="boolean" required="false" default="True">  <!--- If this is set to False then only the address will be output, not a mailto link. --->
<CFARGUMENT name="LinkText" type="string" required="false" default="">  <!--- If you want alternate text for the linked text you can enter it here. --->
<!---
	###################################################
	## Author: George Jaros                          ##
	## Script Name: ColdFusion EncodedEmail Function ##
	## Copyright 2007 George Jaros                   ##
	## www.georgejaros.com                           ##
	## This code may be replicated as long as        ##
	## this header statement is included.  The       ##
	## Instructions below may be removed.            ##
	## Special thanks to Project Honeypot            ##
	## www.projecthoneypot.org                       ##
	###################################################
--->


<CFSET arrEmail = ArrayNew(1)>
<CFSET arrEmail[1] = ListFirst(EmailAddress,"@")>
<CFSET arrEmail[2] = ListFirst(ListLast(EmailAddress,"@"),".")>
<CFSET arrEmail[3] = ListFirst(ListLast(EmailAddress,"."),"?")>
<CFIF ListLen(EmailAddress,"?") GT 1><CFSET arrEmail[4] = ListLast(EmailAddress,"?")><CFELSE><CFSET arrEmail[4] = ""></CFIF>
<CFSET strEncodedEmail = "<script type='text/javascript'>var a = new Array('&##46;#arrEmail[3]#','#arrEmail[1]#','&##64;#arrEmail[2]#','">
<CFIF Trim(arrEmail[4]) NEQ "">
	<CFSET strEncodedEmail = strEncodedEmail & "&##63;#arrEmail[4]#">
</CFIF>
	<CFSET strEncodedEmail = strEncodedEmail & "');document.write(""">
<CFIF MailTo>
	<CFSET strEncodedEmail = strEncodedEmail & "<a">
	<CFIF Trim(CSS_Class) NEQ "">
		<CFSET strEncodedEmail = strEncodedEmail & " class='#CSS_Class#'">
	</CFIF>
	<CFSET strEncodedEmail = strEncodedEmail & " href='&##109;&##97;&##105;&##108;&##116;&##111;&##58;""+a[1]+a[2]+a[0]+a[3]+""'>">
</CFIF>
<CFIF Trim(LinkText) NEQ "">
	<CFSET strEncodedEmail = strEncodedEmail & """+'" & LinkText & "'+""">
<CFELSE>
	<CFSET strEncodedEmail = strEncodedEmail & """+a[1]+a[2]+a[0]+""">
</CFIF>
<CFIF MailTo>
	<CFSET strEncodedEmail = strEncodedEmail & "</a>">
</CFIF>
<CFSET strEncodedEmail = strEncodedEmail & """);</script>">
<CFRETURN strEncodedEmail>
</CFFUNCTION>
<!--- ============ END ENCODEDEMAIL() ============ --->


eXTReMe Tracker