Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
Web Dev + WordPress + Security

Spamless Email Address via JavaScript

[ Mr. T ] Let’s face it, spam sucks. Give spammers the figurative finger by using this nifty bit of JavaScript to hide your email address from the harvesters. Here is an easy copy-&-paste snippet for including a spam-proof email address in your web pages. Although there are a million ways of doing this, I am posting this for the record (and because I just can’t stand deleting usable code). This technique uses JavaScript, and therefore is not 100% ideal for all users. My advice would be to include a <noscript> element that contains an image of your email address. That way, users without JavaScript will still have access to your (spam-proof) email address. Of course, image-based text presents issues for text-only browsers, but hey, you gotta start somewhere! ;)

Spamless Email

Secure the function by adding this block of code to the document <head>, or by placing it within an external JavaScript file:

<script type="text/javascript">

function email(name, domain, extension) {
   if (!document.write) return false;
   if (document.write) {
      var name; var domain; var extension;
      document.write('<a href="' + 'mailto:' + name + '@' + domain + '.' + extension + '">' + name + '@' + domain + '.' + extension + '<\/a>');


Then to replace the three variables with real values, add this to the document body element:

<script type="text/javascript">

email("name", "domain", "extension");


Finally, you may also add this line for those without JavaScript:

<noscript><p>(enable javascript or view source to see the email link)</p></noscript>

Or, you may wish to create a text-image of your email address for display on non-JavaScript machines:

   <img src="images/email.gif" alt="Email Address" class="noscript" style="border: 0px;" />

Specific Example

Here is an example of how the function may be configured on an actual web page. This is the specific code that we use at Monzilla Media to hide email addresses from the spam holes:

<script type="text/javascript">

function email(m, o, n) {
   if (!document.write) return false;
   if (document.write) {
      var m; var o; var n;
      document.write('<p>Contact: <a href="' + 'mailto:' + m + '@' + o + '.' + n + '?subject=General%20Business" title="Contact">' + m + '@' + o + '.' + n + '<\/a><\/p>');


<script type="text/javascript">

email("m0n", "monzilla", "biz");


<noscript><p>(enable javascript or view source to see the email link)</p></noscript>

It’s just good times.

Jeff Starr
About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
GA Pro: Add Google Analytics to WordPress like a pro.

9 responses to “Spamless Email Address via JavaScript”

  1. Very useful tutorial man was thinking of gaps in my security;
    On a unrelated note how did you code the access menus on top?

  2. Jeff Starr
    Perishable 2008/03/05 3:32 pm

    The access menu located at the top of the page? Good question. Rather than post the process here, I will write a complete tutorial that explains the technique and share it with the community. Thanks for the idea!

  3. Jeff Starr

    ..and done! Check out the hover/access menu tutorial here.

  4. great help!!!!!

  5. I was going to mention this on your more recent Industrial-Strength
    Spamless Email Links article, but comments on that are closed.

    I have no fears about placing my email address
    anywhere, as I am a long term SpamAssassin user.

    The one worry is that if I place my email address
    here, people could use it to forge comments by me.

    But I’m not going to worry about that. Anyways, I place a zero burden
    on anyone trying to get in touch with me: “customers come first.”

    By the way, I note you have both

    – Contact Jeff via email
    – Contact Jeff via form

    links, however both just lead to the latter, at least here in my
    emacs-w3m (like lynx) browser,

  6. Jeff Starr
    Jeff Starr 2009/02/21 7:36 pm

    Hi jidanni, I have heard of, but have yet to try SpamAssassin. It must be effective if you feel so confident giving out your email in such a bold fashion. Perhaps it’s worth a look..

    As for the duplicate contact links, they are intentional. In JavaScript-enabled browsers, my actual email address is displayed via script. When JavaScript is not available, I use the contact form as a fallback.

    Very attentive! ;)

  7. Well I HAVE NEVER… viewed your site outside of my emacs-w3m text browser, :-)

    Therefore CHECKMATE, your site is not fair and square accessibility-wise :-)

    Not that I can’t just use Firefox… I am just too nerd-like :-)

  8. Jeff Starr
    Jeff Starr 2009/02/21 8:01 pm

    LOL – You got me!! ;)

  9. really good article on spam thanks!!!!

Comments are closed for this post. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
.htaccess made easy: Improve site performance and security.
Part of my success is from being able to look ahead down the road and see things coming before they actually get here.
Spent some time refreshing my business portfolio at :)
Tried lots of apps for making animated GIFs from screen captures, so far Kap is my favorite. For example the GIF on this post.
Autumn is my favorite time of year.
Stoked! Had a great interview with Eric over at :)
Air finally clearing here in WA. Feeling grateful to breathe again. #oxygenmatters
Past week here in WA state has been hellish. So much smoke, like living in a chimney.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.