[removed] Change the function of the browser's back button

后端 未结 4 1652
情深已故
情深已故 2020-11-30 07:22

Is there a way to make the user\'s back button on their browser, call a javascript function instead of going back a page?

4条回答
  •  离开以前
    2020-11-30 08:15

    yes, you can. Use this js:

    (function(window, location) {
        history.replaceState(null, document.title, location.pathname+"#!/stealingyourhistory");
        history.pushState(null, document.title, location.pathname);
    
        window.addEventListener("popstate", function() {
          if(location.hash === "#!/stealingyourhistory") {
                history.replaceState(null, document.title, location.pathname);
                setTimeout(function(){
                  location.replace("http://www.programadoresweb.net/");
                },0);
          }
        }, false);
    }(window, location));
    

    That will redirect your back button to the location.replace you specify

提交回复
热议问题