Close pop up on back button

后端 未结 10 1511
长发绾君心
长发绾君心 2020-12-08 08:15

I want to close pop up on click of back button for mobile. I implemented this using onhashchange:

window.onhashchange = function (event) {

};
相关标签:
10条回答
  • 2020-12-08 08:36

    Issue : When a modal is open and the user clicks browser's back button, the page navigates (backwards or forward) but the modal remains open.

    Solution :

    In Javascript :

    $(window).on('popstate', function() {
      $('.modal').click();     
    });
    

    In Angular :

      ngAfterViewInit() {
    $(window).on('popstate', function() {
      $('.modal').click();     
    });}
    
    0 讨论(0)
  • 2020-12-08 08:37

    By clicking back, automatically $('.modal').hide() function get activated. So no need to hide modal. We can see grey shaded background after back button.You can use either of these line of code to close modal pop up.

    1. $('.modal' ).modal( 'hide' ).data( 'bs.modal', null ); [work on bootstrap 3]

    Or

    1. $('body').removeClass('modal-open');

      $('.modal-backdrop').remove();

    Inspect the page when modal is active you can see these classes. Do correct me if this method is wrong or other simple way exist.

    0 讨论(0)
  • 2020-12-08 08:38

    I write this code for my own website

    and tested too many times with different devices and browsers

    Chromium 71 , Chrome 67 , FireFox 65 , Android 4.1 , Android 4.2 , Android 7.1

    window.onload=function(){
    
        State=0;
    
        $(".modal").on("show.bs.modal",function(){
            path=window.location.pathname+window.location.search;
            history.pushState("hide",null,path);
            history.pushState("show",null,path);
            State="show";
        })
        .on("hidden.bs.modal",function(){
            if(!!State)
                history.go(State=="hide"?-1:-2);
        });
    
        setTimeout(function(){// fix old webkit bug
            window.onpopstate=function(e){
                State=e.state;
                if(e.state=="hide"){
                    $(".modal").modal("hide");
                }
            };
        },999);
    
        $("#myModal").modal("show");
    };
    
    • dont use $(document).ready instead of window.onload
    0 讨论(0)
  • 2020-12-08 08:44

    bootply.com was down when I was testing my answer. See the inline script and comments at the bottom of the code below. The rest is just Twitter Bootstrap boilerplate so that I could easily test it locally.

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>modal.html</title>
        <!-- Bootstrap -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
      </head>
      <body>
        <p>If you press the back button now, you should return to whatever page you were on before this page.</p>
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Show me the modal!</button>
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
              </div>
              <div class="modal-body">
                <p>If you press the web browser's back button OR the modal's close buttons, the modal will close and the hash will return to "modal.html#modalClosed".</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    
        <script type="text/javascript">
          // Immutable hash state identifiers. 
          var closedModalHashStateId = "#modalClosed";
          var openModalHashStateId = "#modalOpen";
    
          /* Updating the hash state creates a new entry
           * in the web browser's history. The latest entry in the web browser's
           * history is "modal.html#modalClosed". */
          window.location.hash = closedModalHashStateId;
    
          /* The latest entry in the web browser's history is now "modal.html#modalOpen".
           * The entry before this is "modal.html#modalClosed". */
          $('#myModal').on('show.bs.modal', function(e) {
            window.location.hash = openModalHashStateId;
          });  
    
          /* When the user closes the modal using the Twitter Bootstrap UI, 
           * we just return to the previous entry in the web 
           * browser's history, which is "modal.html#modalClosed". This is the same thing
           * that happens when the user clicks the web browser's back button. */
          $('#myModal').on('hide.bs.modal', function(e) {
            window.history.back();
          });      
        </script>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题