Home > The Newbie Zone > Questions and Answers

Need a back button script that works (4)


11-04-2015 11:50 AM #1 sawrubh (Member)
Need a back button script that works

Hey,

I've tried many back button scripts available on STM and even outside (on StackOverflow and other websites) but none of them have worked. Does someone have any back button script which actually works on Chrome on Android?


11-04-2015 12:01 PM #2 stngrm (Member)

This one definitely works:

Code:
<script type="text/javascript">
(function(window, location) {
    history.replaceState(null, document.title, location.pathname+"#!/backbutton");
    history.pushState(null, document.title, location.pathname);
    window.addEventListener("popstate", function() {
      if(location.hash === "#!/backbutton") {
        history.replaceState(null, document.title, location.pathname);
        setTimeout(function(){
          location.replace("http://your.tracking.link/");
        },0);
      }
    }, false);
}(window, location));
</script>


11-04-2015 07:28 PM #3 sawrubh (Member)

Thanks, that certainly works Additionally, what do I need to do if I want to not allow the user to go anywhere at all after pressing the back button, I mean I want him to stay on the same exact page, I tried replacing the tracking link by # but that only keeps the user on the current page for one back button press, if the user presses the back button twice, he goes out. Is there a way to keep the user stuck on the page?

Besides this, do you have a way to make alert/beep sound work when the page load? I tried using the audio tag with autoplay but it doesn't work on Chrome.


11-05-2015 07:45 AM #4 sawrubh (Member)

I found a snippet which allows to keep the user on the same page even after multiple back button presses:

Code:
<script type="text/javascript">
          history.pushState({ page: 1 }, "title 1", "#nbb");
          window.onhashchange = function (event) {window.location.hash = "nbb";};
</script>


Home > The Newbie Zone > Questions and Answers