, ,

jQuery Confirm mailto: A plugin to confirm with users before opening any mailto link.

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:

messageStringDefault : Do you want to send an email $to? – Message to display in the confirmation dialog. $to is the recipient format.
toStringDefault : href – The format of the recipient in the dialog message. ‘href’ : email linked to in href attribute / ‘html’ : inner html of anchor tag.
successFunctionCalled on successful confirmation, before redirection.
failFunctionCalled on failed confirmation.
callbackFunctionCalled after success/fail and after redirection.

Methods:

confirmMailtoFunctionReturns : jQuery object – Main call

Screenshots:

Creating a JavaScript Library

I recently wrote a JavaScript library for creating random colors and color schemes, and while I foun…

jQuery Flash Plugin

This jQuery plugin is for embedding Flash movies. Pages are progressively enhanced when Flash and Ja…

jQuery Optimization: Optimize Selector

We can also optimize our selector other than being a bit specific on our selector. The key to optimi…

jQuery drop down menu: droppy

Quick and dirty nested drop-down menu in the jQuery style. I needed a nav like this for a recent pro…

jQuery slideViewerPro: A “pro” jQuery image slider

slideViewerPro is a fully customizable jQuery image gallery engine which allows to create outstandin…

jQuery Optimization: Avoid Unnecessary Styling

This is another common mistake that many jQuery developer made. jQuery provides us with the ability …