kevinleary.net

jQuery Mailto Links Plugin: Version 1.1

Another revised edi­tion of the jQuery Mailto Plu­gin has arrived. This one includes in the fol­low­ing updates:

  • REGEX replace­ment
  • Updated sta­tus message
  • Sim­pli­fied usage using a class rather than the rel attribute to tar­get mailto: links.
  • Leaner code

XHTML

1
<a href="info(at)kevinleary.net" class="email">info(at)kevinleary.net</a>

JavaScript / jQuery Plugin

1
2
3
4
5
6
jQuery.fn.mailto = function() {
	return this.each(function(){
		var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
		$(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
	});
};

Sim­ple Usage

1
$('.email').mailto();

Above is the eas­i­est way to setup this plu­gin. Of course, I pre­fer to add a lit­tle more logic to make things more intuitive.

Advanced Usage

1
2
3
4
5
if ($('a.email').length > 0){
	$.getScript('/path/to/jquery.mailto.js',function(){ 
		$('a.email').mailto();
	});
}

What this does is first check to see if there are any anchor links in the doc­u­ment that have a class of “email”. If there are, we’ll load in the jquery.mailto.js file, and run a func­tion once it has been loaded. This func­tion (or call­back) runs the mailto() method on all email anchors.

Why use REGEX in this version?

I’ve used REGEX in place of replace() for this ver­sion because it ren­ders the browser mes­sage to dis­play emails cor­rectly. With my pre­vi­ous ver­sions some users could see “you[at]domain[dot]com” in the bot­tom left mes­sage of their browsers. REGEX takes care of this lit­tle mishap and dis­plays the ren­dered email address.

Down­load

jQuery Mailto Plu­gin (.js)

Resources

25 Comments

  1. Brad / 2.1.09 / 11:32 AM

    Thanks so much for this. I wanted some­thing that all I would have to do is copy/paste a cou­ple lines of code and not have to worry about it. This did the trick. Thanks

  2. kevin / 2.1.09 / 11:58 AM

    I always try to make my scripts easy to imple­ment. If there was any step that was dif­fi­cult or con­fus­ing to you please let me know. Also if you think some­thing could have been eas­ier, or would like to have an added fea­ture let me know and I can work it in there for you.

  3. karl / 2.27.09 / 4:17 PM

    Hi Kevin, thanks for the plugin…it looks to be really help­ful, but I’m hav­ing an issue imple­ment­ing it. Per­haps there’s some­thing I’m miss­ing, as I’m rel­a­tively new to using jquery. Here’s the dev site: http://www.coidesign.com/dev/metta/book-appointment.php. Any ideas?

  4. karl / 2.27.09 / 4:18 PM

    I’m sorry, that link is

    http://www.coidesign.com/clients/metta/book-appointment.php

    It just looks like the char­ac­ters aren’t being replaced. What am I doing wrong? Thanks!

  5. karl / 2.27.09 / 4:27 PM

    Nevermind…I got it func­tion­ing. Thanks for the great plugin!

  6. Adam / 6.29.09 / 2:22 PM

    The down­load link to the JS file is broken.

  7. kevinlearynet / 6.29.09 / 2:52 PM

    Sorry about that adam, some of my Word­Press uploads were point­ing to an old direc­tory, they should all be fixed now

  8. BoonKwee / 7.18.09 / 7:39 AM

    Could you post an exam­ple of how this is used for those of us that are cod­ing chal­lenged? Thanks!

  9. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  10. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  11. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  12. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  13. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  14. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  15. kevinlearynet / 7.20.09 / 12:56 PM

    Sure Boon, Here’s one I setup at my day job:

    http://www.freshtilledsoil.com/web-resources/wp-s...

    I hope that helps! Let me know how the setup goes.

  16. Erik / 9.17.09 / 10:40 PM

    inter­est­ing. I’ve never seen this getScript() method before. nice.

  17. tamika parks / 10.30.09 / 8:54 PM

    Hello just fig­ured i will let you know i also had a prob­lem with this blog com­ing up frozen as well. Must be mon­keys in the system.

  18. kevinlearynet / 11.1.09 / 6:46 PM

    Maybe it’s time for a new sys­tem, one with­out the monkey’s of course. Does any­one out there have any sug­ges­tions for a fast cloud or ded­i­cated host that sup­ports Word­Press well?

  19. Paul Alkema / 11.23.09 / 2:25 PM

    Link is broken. :)

  20. kevinlearynet / 11.23.09 / 2:59 PM

    Sorry about that, try this out:

    http://www.kevinleary.net/wp-samples/mailto.html

  21. Paul Alkema / 11.23.09 / 3:49 PM

    It’s a great post though, Thanks for your con­tri­bu­tion to the community. :)

  22. Paul Alkema / 11.23.09 / 3:48 PM

    You may want to note that…

    $(‘.email’).mailto();

    should be executed..

    $(document).ready(function(){
    $(‘.email’).mailto();
    });

  23. Sam / 11.25.09 / 5:18 AM

    Thanks for this… works great!

  24. kevinlearynet / 1.20.10 / 1:38 PM

    You may want to check this out Erik:

    http://www.kevinleary.net/external-javascript-on-...

  25. axel jacobs / 6.21.10 / 9:08 AM

    Hi

    thanks for the script

    I changed the code a bit so the link can be some­thing else like ‘con­tact us’.

    jQuery.fn.mailto = func­tion() {
    return this.each(function(){
    var email = $(this).attr(“href”).replace(/s*(.+)s*/, “@”);
    $(this).before(‘<a href=“mailto:’ + email + ’” rel=“nofollow” title=“Email ’ + email + ’”>’ + $(this).html() + ”).remove();
    });
    };

Leave a comment

will not be published

Wrap code blocks with <pre lang="LANGUAGE" line="1"> and </pre> where LANGUAGE is a GeSHi supported language syntax.