Email Protection with MooTools JavaScript v2

By  on  

Earlier this week I posted an article describing how you can protect your email links from spambots using MooTools. After some suggestions, I've made some improvements to my system.

The XHTML

	
	<span rel="david|davidwalsh.name" class="email custom-class">Email me!</span>
	

I've switched to using a span tag instead of an anchor to prevent search engines from seeing dead links. We put the modified email address in the rel attribute.

The MooTools JavaScript


window.addEvent('domready', function() {
	$$('.email').each(function(el) {
		var anchor = new Element('a', {
			href: 'mailto:' + el.get('rel').replace('|','@'),
			'class': el.get('class'),
			'text': el.get('text')
		}).replaces(el);
	});
});

I create an anchor element and replace the span element that was originally there.

Thank you to everyone that shared their ideas -- we came up with a better script!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    MooTools Text Flipping

    There are lots and lots of useless but fun JavaScript techniques out there. This is another one of them. One popular April Fools joke I quickly got tired of was websites transforming their text upside down. I found a jQuery Plugin by Paul...

  • By
    HTML5&#8217;s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

Discussion

  1. This version is way cleaner, I assume I can change the pipe to any weirdo special character like &#98237980127r5981327r0981; then do the replace?

  2. neutral

    Have you tested how effective is it against spam yet?

    If it picks up any spam at all, I guess the next step would be to add in ROT13 to your approach.

  3. Internet explorer 7 doesn’t like the “,” after ‘class’: el.get(‘class’)
    That throws an error…

  4. Birke

    I tried to use this but my Browser does nothing if I click on the E-Mail me text.

  5. @Birke: Works for me.

  6. Wes

    I had to add ‘text’: el.get(‘text’) to the new anchor element to be able to show the “Email Me” text.

  7. @Wes: Good catch — updated that and compatibility with MooTools 1.3 (removed $each).

  8. Thanks! The only thing i did different was to use title instead of rel. rel isn’t valid in a span.

  9. Greetings,

    I am new to mootools and I would like to know were I put the java script code for this?

    Thanks for any help.

  10. Perhaps this might help some. For strict xhtml validation if possible use the id attribute and not rel and use a – in replace if |. Making these changes will allow proper validation on strict xhtml.

    Tim

    BTW I figured out were to put the code :)

  11. Perhaps this might help some. For strict xhtml validation if possible use the id attribute and not rel and use a – in replace of |. Making these changes will allow proper validation on strict xhtml.

    Tim

    BTW I figured out were to put the code :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!