Confirm with users before opening any mailto: link through their default email client.
A jQuery plugin.
Make sure you have jQuery loaded properly.
<script src="http//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Include confirm.min.js or place the plugin code at the top of your javascript file (recommended).
!function(t){t.fn.confirmMailto=function(e){var n=t.extend({message:"Do you want to send an email to $to?",to:"href",callback:function(){},success:function(){},fail:function(){}},e),a=/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi,i=function(e){var i=n.message,c=n.to;if(c="href"==c?t(this).attr("href").match(a):"html"==c?t(this).html():c,i=i.replace("$to",c)==i?i+c:i.replace("$to",c),confirm(i)){n.success();var o=!0}else{e.preventDefault(),n.fail();var o=!1}return setTimeout(function(){n.callback(o)},1),o};return this.filter('[href^="mailto:"]').each(function(){t(this).bind("click",i)}),this}}(jQuery);
Call the method on document load.
$(document).ready(function(){
$('a').confirmMailto();
});
That's it.
You can alternately call the method with the following options.
$('a.advanced').confirmMailto({
message: 'Are you cool enough to send an email to $to? ',
to: 'html', // href/html
success: function(){
$('a.advanced').css('color','#3C3');
},
fail: function(){
$('a.advanced').css('color','#F66');
},
callback: function(result){
if(result){
alert('Thank you!');
}else{
alert('Boooo!');
}
}
});