Scott Hurring » Code » PHP » Email Protect

Download Code
download
email_protect-v0.2.tgz
v0.2 alpha · Apr 06, 2005

Description

About a month after i first setup hurring.com, i decided that i couldn't just throw regular email addresses up on these pages or i'd risk having them harvested by spambots and have tons of lovely jiggling spam flowing my way. This code outputs an email address as simple Javascript code that any javascript-compliant browser should be able to understand.

Essentially, it encodes the "@" symbol as unescape("%40"). ("%40" is simply "@" run through the escape() function, which we reverse with a call to unescape function, yeilding "@" back again). It encodes "." as unescape("%2e"). This way, any browser that has javascript enables will properly print the "@" symbol, whereas a spam-bot that looks at the page code will see unescape("%40")

Example

// A call to
email('scott at hurring dot com');

// Generates this Javascript code
<script type='text/javascript'>
/* Javascript code to print out spam-proof email address */
a = unescape('%40'); d = unescape('%2e');
lt = unescape('%3c'); gt = unescape('%3e');
document.write(lt+'a title="Email: scott (at) hurring (dot) com" 
	href="mailto:scott'+
	a+'hurring'+d+'com"'+gt+'scott (at) hurring (dot) com'+lt+'/a'+gt);
</script>

// Which the browser displays as:


Download

v0.2 - Apr 06, 2005

Changelog / History