views:

45

answers:

2

I'm creating a plugin that replaces alerts/confirms for a project and I was curious if there was a way to make it like a real confirm where you can do:

if(confirm('Yes or no?')){alert('You agreed!');}

Right now I could do with a call back using this syntax:

$.alert('yes or no',{type:'confirm'});

But i want to be able to do:

if($.alert('yes or no',{type:'confirm'})){/*some action on true*/}

Here is what I have so far and look for the all CAPS comments in the click event (remember, this is still in development, so the HTML and stuff is still a little icky):

(function($) {
    $.alert = function(message,options) {
        defaults = {
            type:'alert',
            callback:function(){}
        }
        options = $.extend({},defaults,options);
        if(options.type == 'confirm'){
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/tick_48.png"&gt;&lt;p&gt;'+message+'&lt;/p&gt;&lt;br class="clear"><span><a class="cancel" href="#cancel">Cancel</a><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        else{
            $('<div style="display:none" class="alerthiddenoverlay"></div><div style="display:none" class="customalertbox"><div><img src="http://cdn.iconfinder.net/data/icons/basicset/warning_48.png"&gt;&lt;p&gt;'+message+'&lt;/p&gt;&lt;br class="clear"><span><a class="ok" href="#ok">OK</a></span><br class="clear"></div></div>').prependTo('body');
        }
        $alertboxclass=$('.customalertbox');
        $alerthiddenoverlay=$('.alerthiddenoverlay');
        $alertboxclass.find('a').click(function(event){
            event.preventDefault();
            var the_return = false;
            if($(this).attr('href') == '#ok'){
                var the_return = true;
            }
            $alertboxclass.fadeOut(250,function(){$alerthiddenoverlay.delay(250).fadeOut(250,function(){$(this).remove();options.callback.call(this,the_return);});$(this).remove()});
        });
        $alertboxclass.css({
            top:$(window).height()/2-$alertboxclass.height()/2,
            left:$(window).width()/2-$alertboxclass.width()/2
        });
        $alerthiddenoverlay.css({height:$(window).height()+'px',width:'100%',position:'fixed',zIndex:'9998'}).fadeIn(250,function(){$alertboxclass.delay(250).fadeIn()});
    }
})(jQuery);
A: 

I saw a nice confirm() overwrite to a modal window as an example of jqModal

Here is the code sample. I'm sure you can adapt it to your need...

/* Overriding Javascript's Confirm Dialog */

// NOTE; A callback must be passed. It is executed on "cotinue". 
//  This differs from the standard confirm() function, which returns
//   only true or false!

// If the callback is a string, it will be considered a "URL", and
//  followed.

// If the callback is a function, it will be executed.


function confirm(msg,callback) {
  $('#confirm')
    .jqmShow()
    .find('p.jqmConfirmMsg')
      .html(msg)
    .end()
    .find(':submit:visible')
      .click(function(){
        if(this.value == 'yes')
          (typeof callback == 'string') ?
            window.location.href = callback :
            callback();
        $('#confirm').jqmHide();
      });
}


$().ready(function() {
  $('#confirm').jqm({overlay: 88, modal: true, trigger: false});

  // trigger a confirm whenever links of class alert are pressed.
  $('a.confirm').click(function() { 
    confirm('About to visit: '+this.href+' !',this.href); 
    return false;
  });
});
pixeline
+1  A: 

I think passing a callback method in as a parameter to the $.alert function is going to be the easiest option. If that's a deal-breaker though, I might look at the .queue() method for chaining the events.

http://api.jquery.com/queue/

nleach