js监听浏览器后退事件

在浏览器中打开页面时,点击手机物理键和浏览器的返回按钮,返回到浏览器的初始化页面,针对这个事情,做了一些处理。

js监听浏览器后退事件

这是自己写的代码,能在微信和浏览器通用,但是UC和夸克浏览器中不太好使

<script>

        var state = {

            title:"手机银行",

            url:'page/index.html'+document.location.hash

        }

        window.history.pushState(state,"手机银行",'page/index.html'+document.location.hash);

        setTimeout(function(){

            window.addEventListener("popstate",function(){

                var state = {

                    title:"手机银行",

                    url:'page/index.html'+document.location.hash

                }

                window.history.pushState(state,"手机银行",'page/index,html'+document.location.hash);

            })

 

        },0);

    </script>