Search code examples
jquerybpopup

How to call a function on beforeclose event in bpopup?


I want to call a function before closing the bpopup. I have used following code:

$('#casepromptPopup').bPopup({
    escClose: false,
    closeClass: 'b-close',
    modalClose: false,
    onClose: function() {                         
        confirm("Are you sure you wish to quit and grade your performance?");

I want to show the confirm message before popup close. But onClose method fires after the popup close.


Solution

  • While I'm not a fan of plugin modifications, it seems that there's no other way to achieve this.

    Modify the bpopup.js:

    • 1. Find function close() and add if(o.confirmClose()){ :

      function close() {
          if(o.confirmClose()){
              // rest of the original function
          }
      }
      
    • 2. Find $.fn.bPopup.defaults = { (at the bottom) and add:

      confirmClose: function(){return true;}
      

    Now you can add confirmation into your bpopup options:

    $('#casepromptPopup').bPopup({
        escClose     : false,
        closeClass   : 'b-close',
        confirmClose : function(){                         
            return confirm("Are you sure you wish to quit and grade your performance?");
        }
    });
    

    JSFiddle