If you need to setup this after the modal is shown, you can use @Nabid solution. However, sometimes you still need to allow some method to close the modal. Assuming you have a button with class really-close-the-modal
, which should really close the modal, you can use this code (jquery):
var closeButtonClicked = false;
$('.really-close-the-modal').on('click', function () {
closeButtonClicked = true;
});
$('#myModal').on('hide.bs.modal', function (e) {
if (!closeButtonClicked) {
e.preventDefault();
e.stopPropagation();
return false;
}
closeButtonClicked = false;
});
This isn't really nice code design, but it helped me in a situation where the modal was shown with a loader animation, until an ajax request returned, and only then could I know if the modal needed to be configured to prevent "implicit" closing. You can use a similar design to prevent closing the modal while it's still loading.