Close pop up on back button

后端 未结 10 1512
长发绾君心
长发绾君心 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: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

提交回复
热议问题