Detect use of Android back button using JavaScript

后端 未结 2 1924
走了就别回头了
走了就别回头了 2021-02-20 05:54

My site has breadcrumbs which highlight which stage in a process the user has reached. The breadcrumbs rely on the browser history to tell which stage should be highlighted when

2条回答
  •  孤城傲影
    2021-02-20 06:44

    1. Use popstate event.

    https://developer.mozilla.org/en-US/docs/Web/Events/popstate

    window.onpopstate = function(e) { 
       updateBreadCrumbObservable();
    };
    

    2. Use onhashchange event.

    window.onhashchange = function(e) {
       updateBreadCrumbObservable();
    }
    

    You can use event argument to get more description about the event fired.

提交回复
热议问题