Say you want to display a contact email on your website – but are worried about crawlers, spammers etc. picking it up and making your life miserable. There’s a few workarounds – but this one is about as elegant as it gets. In effect, instead of writing out your email inside the mailto: tag, let some javascript code spit it out for you (javascript is good at spitting out html). Not only that – but let it construct it for you from broken up components (e.g. – for an email address = joe@joessite.com you just pass it joe, joessite and 0 – the 0 denotes a .com). The javascript will construct the full email address for you from these pieces. In effect, your actual email address is not listed ANYWHERE on the site (it is created dynamically by javascript), so there’s no chance of a crawler, spammer being able to read it.

  1. Include the following javascript (emailconcealer.js) in your file (either inline or referenced).

 

Code Snippet
  1. // EmailConcealer.js
  2.  
  3. var tld_ = new Array()
  4.  
  5. tld_[0] = "com";
  6.  
  7. tld_[1] = "org";
  8.  
  9. tld_[2] = "net";
  10.  
  11. tld_[3] = "ws";
  12.  
  13. tld_[4] = "info";
  14.  
  15. tld_[10] = "co.uk";
  16.  
  17. tld_[11] = "org.uk";
  18.  
  19. tld_[12] = "gov.uk";
  20.  
  21. tld_[13] = "ac.uk";
  22.  
  23. var topDom_ = 13;
  24.  
  25. var m_ = "mailto:";
  26.  
  27. var a_ = "@";
  28.  
  29. var d_ = ".";
  30.  
  31.  
  32. function mail2(name, dom, tl, params, display) {
  33.  
  34.     document.write('<a href="' + m_ + e(name, dom, tl) + params + '">' + display + '</a>');
  35.  
  36. }
  37.  
  38. function e(name, dom, tl) {
  39.  
  40.     var s = name + a_;
  41.  
  42.     if (tl != -2) {
  43.  
  44.         s += dom;
  45.  
  46.         if (tl >= 0)
  47.  
  48.             s += d_ + tld_[tl];
  49.  
  50.     }
  51.  
  52.     else
  53.  
  54.         s += swapper(dom);
  55.  
  56.     return s;
  57.  
  58. }
  59.  
  60. function swapper(d) {
  61.  
  62.     var s = "";
  63.  
  64.     for (var i = 0; i < d.length; i += 2)
  65.  
  66.         if (i + 1 == d.length)
  67.  
  68.             s += d.charAt(i)
  69.  
  70.         else
  71.  
  72.             s += d.charAt(i + 1) + d.charAt(i);
  73.  
  74.     return s.replace(/\?/g, '.');

 

2.  If you are inside an ASP.NET content page (or master page), you will need to reference the script within the Page_Load of the content page (or master page). Otherwise, proceed to step 3.

Code Snippet
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         Page.ClientScript.RegisterClientScriptInclude(“selective”, ResolveUrl(@”scripts\emailconcealer.js”));
  4.         if (!Master.Page.ClientScript.IsStartupScriptRegistered(“alert”))
  5.         {
  6.             Master.Page.ClientScript.RegisterStartupScript
  7.                 (this.GetType(), “alert”, “insideJS();”, true);
  8.         }
  9.     }

3. Insert the following <script> code in your aspx (web page)

Email: <script type=”text/javascript”>mail2(“joe”, “joessite”, 0, “”, “Contact Joe”)</script>

See an example in action

See the email link on my contact form on this website for a live example.

http://cut.ms/bne8

Summary

Don’t be intimidated by spammers, crawlers into hiding your email address on your website. There’s a way to have your cake (put your email link) and eat it too (not let crawlers decipher it).

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.