Benny Sutton is 4 hire - just click here

Hide email address using JavaScript

Obfuscate email address JavaScript

Sick of SPAM? If you don't want to openly display your email address in links on your site use JavaScript to obfuscate it like this...

See the working demo below.

Demo: Obfuscate email with JavaScript

JavaScript

This is pretty much the standard JavaScript you'll paste into your page script block - or in a .js file if you prefer.

             
            function mailme(address, subject, body) {
              // here we replace the @ symbol with ~ symbol 
              // and the dot in the email address and ::
              // you do have to do the reverse to your email address
              // before you pass it in but it really hides it!
                address = address.replace(/::/g, '.');
                self.location.href = "mailto:" + address.replace('~', '@') + "?subject=" + subject + "&body=" + body;
            }  
            
            // Another, better version that works with the C# below
            function mailMe(address, subject, body) {
	            var addressout = address.split('').reverse().join('')
	            self.location.href = "mailto:" + addressout.split('|').reverse().join('@') + "?subject=" + subject + "&body=" + body;
            }

        
    

C# MVC code server side

If your email is coming from a database you can encode it like this - basically the reverse engineer of the javascript above. It is a custom HTML helper

             
        public static IHtmlString MailMe(this HtmlHelper helper, string address, string linktext, string title, string body, string cssClass = "", bool showButton = false)
        {
            if (!address.IsEmail())
            {
                return new MvcHtmlString("");
            }
            string reversedAddress = string.Join("|", address
                        .Split('@')
                        .Select(x => new String(x.Reverse().ToArray())));

            StringBuilder sb = new System.Text.StringBuilder();
            if (showButton) { sb.AppendLine("<span class=\"comment-container\"><span class=\"inner\">"); }
            sb.AppendFormat("<a href=\"javascript:void(0)\" onclick=\"mailMe('{0}','{1}','{2}')\" class=\"{4}\"><span style='white-space: nowrap;'>{3}</<span></a>", reversedAddress.JSafe(), title.JSafe(), body.JSafe(), linktext, cssClass);
            if (showButton) { sb.AppendLine("</span></span>"); }
            return new MvcHtmlString(sb.ToString());
        }
        
    

Conclusion

It's easy and more secure to hide email address using javascript