Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When browser back button is clicked, bootstrap modal is closed but modal-dropback stays

I am using Backbone with bootstrap. Problem is when you go through application and in some point you open bootstrap modal window and press back button, modal window closes but modal background div (modal-backdrop) stays and does not disappear. It overlays whole screen and you cant click on anything. I noticed that when you close modal normally modal-backdrop div is removed from html and in this situation it stays.

I was searching for this solution on web, and found similar things, but none of them were tide with browser back button pressed.

I thought about catching browser back button event and user jquery to remove that div, but that is not really good solution.

Can someone point out some solutions for this problem? Or at last tell me why is that happening.

EDIT: When back button is pressed, modal does not throw hide.bs.modal event, so i cant catch it and remove modal-backdrop div

like image 465
toskebre Avatar asked Sep 10 '14 09:09

toskebre


People also ask

Should Back button close modal?

When the modal dialog is open, pressing the back button closes it. On a multi-step modal dialog, the back button can even navigate backwards through the steps! On most web apps, pressing the back button while a modal dialog is open will navigate to the previous page, rather than closing the modal.

How do I keep my Bootstrap modal from closing when I click the button?

To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.

How do I stop Bootstrap modal pop up?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

Do not close modal popup Bootstrap outside click?

On Options chapter, in the page you linked, you can see the backdrop option. Passing this option with value 'static' will prevent closing the modal. As @PedroVagner pointed on comments, you also can pass {keyboard: false} to prevent closing the modal by pressing Esc . Save this answer.


2 Answers

We encountered this problem in Web and Mobile Application developed using AngularJS_v1.4.7 and Bootstrap_v3.0. The expected behavior (across the application) was, Popup should close and page transition should NOT happen. In other words, back button just closes the popup and nothing else.

Below fix worked fine,

//Detect location change
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
        // Select open modal(s)
        var $openModalSelector = $(".modal.fade.in"); 
        if( ($openModalSelector.data('bs.modal') || {}).isShown == true){
            // Close open modal(s)
            $openModalSelector.modal("hide");
            // Prevent page transition
            event.preventDefault();
        }
    });

This fix was tested successfully on,

  • Chrome browser on desktop
  • Windows 8.1 Phone
  • Android Lollipop
like image 129
Gaurang Patel Avatar answered Sep 18 '22 06:09

Gaurang Patel


Try this.Its works for me.

<script type="application/javascript">
    $(window).on('popstate', function() {
        $('.modal').modal('hide');
        $( ".modal-backdrop" ).remove();
        $( ".in" ).remove();
    });
</script>
like image 41
Murali Avatar answered Sep 20 '22 06:09

Murali