Concealing email on your website from web crawlers and spammers
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.
- Include the following javascript (emailconcealer.js) in your file (either inline or referenced).
- // EmailConcealer.js
- var tld_ = new Array()
- tld_[0] = "com";
- tld_[1] = "org";
- tld_[2] = "net";
- tld_[3] = "ws";
- tld_[4] = "info";
- tld_[10] = "co.uk";
- tld_[11] = "org.uk";
- tld_[12] = "gov.uk";
- tld_[13] = "ac.uk";
- var topDom_ = 13;
- var m_ = "mailto:";
- var a_ = "@";
- var d_ = ".";
- function mail2(name, dom, tl, params, display) {
- document.write('<a href="' + m_ + e(name, dom, tl) + params + '">' + display + '</a>');
- }
- function e(name, dom, tl) {
- var s = name + a_;
- if (tl != -2) {
- s += dom;
- if (tl >= 0)
- s += d_ + tld_[tl];
- }
- else
- s += swapper(dom);
- return s;
- }
- function swapper(d) {
- var s = "";
- for (var i = 0; i < d.length; i += 2)
- if (i + 1 == d.length)
- s += d.charAt(i)
- else
- s += d.charAt(i + 1) + d.charAt(i);
- 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.
- protected void Page_Load(object sender, EventArgs e)
- {
- Page.ClientScript.RegisterClientScriptInclude(“selective”, ResolveUrl(@”scripts\emailconcealer.js”));
- if (!Master.Page.ClientScript.IsStartupScriptRegistered(“alert”))
- {
- Master.Page.ClientScript.RegisterStartupScript
- (this.GetType(), “alert”, “insideJS();”, true);
- }
- }
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.
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).
Leave a Reply