0

I want to disable browser back button. So I used below code

  <script type = "text/javascript" >
        history.pushState(null, document.title, location.href);
        window.addEventListener('popstate', function () {           
            history.pushState(null, document.title, location.href);
        });
</script>

It was disable browser back button but Afert submit a page and refresh that page there was a error. page not found.

Dil Dil
  • 3
  • 2
  • _"disable browser back button"_ - **do not do this**. Instead use the unload event and present the user with an option to cancel. https://stackoverflow.com/questions/7317273/warn-user-before-leaving-web-page-with-unsaved-changes – evolutionxbox Aug 09 '17 at 08:10
  • Honestly, I see no way disabling the back button could serve **_ANY_** legitimate functionality at all. You either are about to make a _very_ bad design choice or you are trying to create one of those scam-sites that try to stop you from leaving. – Crowley Astray Aug 09 '17 at 08:12
  • **give me good solution for disable** – Dil Dil Aug 09 '17 at 08:18

1 Answers1

0

I found this solution https://codepen.io/dhavalt10/pen/rGLBzB (not mine) while investigating this problem and worked fine for me. I tested on the latest Chrome, Edge, IE and Firefox.

Basically the js code force the browser to keep on the same page always and Refresh also worked fine.

history.pushState(null, null, location.href);
window.onpopstate = function () {
    history.go(1);
};