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

Comments


Denmark Lån Penge 
August 1. 2009 06:57
Thanks a lot for this post


United States devix 
August 4. 2009 16:38
Nice information


Poland PE 
September 6. 2009 14:43
You have a little mistake there: "jajascript".


September 6. 2009 16:42
Oops! I invented a brand new scripting language Laughing Thank you for pointing that out! Just correctet it!


United States Brad 
July 26. 2010 17:06
So simple yet effective, love it.

I'm using <a class="email" href="mailto:info*{*}*mydomain.com">email</a>. I think the longer and more abstract the string replacement the better.


Germany HCS 
October 17. 2010 14:10
So hopefully my E-Mailadress is now save!
Thanks a lot!

Add comment



  Country flag

biuquote
  • Comment
  • Preview
Loading