Back button redirect script

后端 未结 2 1437
名媛妹妹
名媛妹妹 2020-12-01 23:17

I am trying to develop a javascript code that when a user clicks on the back button of a webpage, it can be redirected to another URL.

I have found these lines of co

2条回答
  •  無奈伤痛
    2020-12-01 23:52

    I have found the answer in case someone else is looking for it.

    Create a separate file called history-stealer.js with the following code:

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

    and then include this code in your HTML file:

    
    

    so that it calls the history-stealer.js file

提交回复
热议问题