an ASP.NET, C# technical blog, by Gianni Tropiano

Anti-spam-crawler e-mail jQuery

by CodeGolem 8. July 2009 20:26

Since I myselft am exploring jQuery potentialities, I was wondering if there could be a simpler way to hide e-mail addresses to web crawlers than writing a specialized control, as I did in a previous post.

Web crawlers are able to read the full html source code, but should not be able to run scripts from the page.

So, finding a way to code e-mail addresses on the source pages, and translating them on the fly using jQuery, should hide the addresses from crawlers.

Let's say, we will use "email" css class to mark all e-mail addresses within our site, and we will write them down as "myaddress at mydomain.com"...
This could be a jQuery function to add on our pages to translate all e-mail addresses on the fly:


<script type="text/javascript">

$(document).ready(function() {
    $('a.email').each(function(i) {
        var text = $(this).text();
        var address = text.replace(" at ", "@");
        $(this).attr('href', 'mailto:' + address);
            $(this).text(address);
        });
    });

</script>

And the following could be an "encoded" email anchor:


<a class="email">myaddress at mydomain.com</a>

That's all... simple enough!

Hope you will find it useful enough... too! Smile

Happy jQuerying!

Tags: , ,

jQuery