C# - Convert Emailaddress in JavaScript to avoid Spam

By , 9/15/2008
(1 ratings)
Convert Emailaddress in JavaScript to avoid Spam

Place this Javascript on your website and your contact address is protected against spam

needed namespaces:

using System.Net.Mail;
using System.Text;
Translate to VB
/// <summary>
/// Gets the JavaScript.
/// </summary>
/// <param name="email">The email.</param>
/// <returns></returns>
private string GetJavaScript(string email)
{
    MailAddress emailadress = new MailAddress(email);
    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.AppendLine("<script language=\"javascript\"><!--");
    stringBuilder.AppendLine(string.Format("var localpart=\"{0}\";", emailadress.User));
    stringBuilder.AppendLine(string.Format("var domainpart=\"{0}\";", emailadress.Host));
    stringBuilder.AppendLine("document.write('<a href=\"mailto:'+localpart+'@'+domainpart+'\">'+localpart+'@'+domainpart+'</a>');");
    stringBuilder.AppendLine("//--></script>");
    stringBuilder.AppendLine(string.Format("<noscript>{0}&nbsp;[at]&nbsp;{1}</noscript>", emailadress.User, emailadress.Host.Replace(".", "&nbsp;[dot]&nbsp;")));

    return stringBuilder.ToString();
}
Tagged with spam, javascript, email.

Comments

 

Log in, to comment!