Angular icon Get $50 off the Angular Master bundle this week only!

Use coupon code ANGULAR_50 at checkout now!

days
hours
mins
secs

Email address Spam Protection with JavaScript

Aug 18, 2012 1 min read

JavaScript post

This is a simple code snippet to prevent any spambots crawling your Contact pages, finding your email address and sending you automated spam. It’s good to include a text-based email address alongside any contact forms you may have. Here’s a quick tutorial to add some simple JavaScript to your website using the document.write command, which prevents spambots sweeping your site for text-based emails.

JavaScript

Here’s what you’d need to add instead of typing out an email address. On page load, the document.write command writes our email variable and small function, and outputs our email address. Since I’ve been using it, I’ve had no spam, even across client websites too.

var emailAddress = ('[email protected]' + 'yourdomain.com');
document.write('<a href="mailto:' + emailAddress + '">' + emailAddress + '</a>');

Disabled JavaScript

For users with JavaScript disabled in their browser, they won’t see an email address, which would definitely be a problem in terms of usability. You could offer two different solutions, a progressive enhancement solution, or using graceful degradation. For purposes of this exercise, we’ll offer a straightforward graceful degradation technique using NoScript. Here’s a few ideas of some solutions, and will all appear for users without JavaScript enabled -

Type out the name without the @ symbol:

    <noscript>name[at]yourdomain.com</noscript>

Use an image of your email address:

    <noscript><img src="img/email-address.jpg" alt="My Email"></noscript>

Integration

All you need to do is include the snippet in your page source, and replace the email address ‘[email protected]’ and ‘yourdomain.com’.

Love the post? Share it!

Lots of time and effort go into creating all our blogs, resources and demos, we'd love if you'd spare a moment to share this one!

About the author

Todd Motto

Google Developer Expert icon Google Developer Expert

Todd is the Founder of Ultimate Courses. With a passion for Angular, TypeScript and JavaScript, Todd leads the online courses creation and has written hundreds of articles on front-end web development and beyond. He specialises in breaking down complex topics and understands the critical mission of learning new technology fast, comprehensively and the right way.