views:

65

answers:

3

I want to display simple confirmation popup box if a user tries to close colorbox. I tried this code:

onCleanup:function(){ confirm('Are you sure'); }

It displays confirmation box but colorbox is closed even if I click "Cancel"!

Can anyone please help?

A: 

I do not know if colorbox allows to cancel the closing once you have started it ..

If it did, though, you would need to change your code to

onCleanup:function(){ return confirm('Are you sure'); }
Gaby
Doesn't work :(
Levani
could always modify ColorBox to add a callback onBeforeClose which takes into account return values from the callback/callbacks
Jonathan Fingland
@Jonathan, indeed. And from a look at the source it is pretty straight forward how to do it ..
Gaby
+2  A: 

I've done something similar with FancyBox. I think your best bet is to bind an event handler to the close button when the ColorBox is displayed:

onComplete:function(){
  $("#cboxClose").click(function(e) {
    // stop any other script from firing
    e.stopPropagation(); 
    if (confirm('Are you sure')) {
      $.colorbox.close();
      // ensure that the binding is removed when closed
      $("#cboxClose").unbind();
    }
  });
}
Ken Earley
A: 

@Ken, your example worked perfectly for me...almost. The only modification that I had to make was to unbind before setting the click function because for some reason, it would ignore my if statement and still close on first load. Below is what I have put into use for a confirmation on closing a colorbox

$(".Add").colorbox({
    onComplete:function(e){
       $("#modalForm").ajaxForm(modalOptions);
       $("#cboxClose").unbind(); 
       $("#cboxClose").click(function(e){
          e.stopPropagation();
          if(confirm('Are you sure you want to cancel your changes?')){
             $.colorbox.close();
             $("#cboxClose").unbind();                               
          }                           
       }); 
    }
   });
Tommy