Confirm with users before opening any link on the page through their default email client.
Download: https://github.com/0xmmo/jquery-confirm-mailto
Demonstration:
Plugin homepage:
Implementation:
Basic Usage:
- 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();
});
Advanced Usage:
- 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!');
}
}
});
Options:
message | String | Default : Do you want to send an email $to? – Message to display in the confirmation dialog. $to is the recipient format. |
to | String | Default : href – The format of the recipient in the dialog message. ‘href’ : email linked to in href attribute / ‘html’ : inner html of anchor tag. |
success | Function | Called on successful confirmation, before redirection. |
fail | Function | Called on failed confirmation. |
callback | Function | Called after success/fail and after redirection. |
Methods:
confirmMailto | Function | Returns : jQuery object – Main call |
Screenshots: