0

I am trying to stop browser back button but my code is not working when click back button it can directly going back how can i do this i am calling ajax not using any form type tag

<script type = "text/javascript" >
 function preventBack() { window.history.forward(); }
            setTimeout("preventBack()", 0);
            window.onunload = function () { null };
    </script>  

I have separate .js files i added in that document.ready function() but it's not working

  • You can't, if browsers allowed scripts to disable history navigation it would result in terrible experiences for the user. – Lennholm May 30 '17 at 10:19
  • But how can i disable browser back button – Bear grills May 30 '17 at 10:45
  • Like I said: you can't – Lennholm May 30 '17 at 10:48
  • Hi @MikaelLennholm, I added previous page to this code it's working. – Bear grills May 30 '17 at 11:08
  • It only works when going back to a history entry that was pushed by you or that represents a fragment on the same page. If the user is going back to a different page there's nothing you can do to prevent that. – Lennholm May 30 '17 at 11:53
  • Disabling back button is bad when you consider UX, but you can prevent the browser from caching the pages though. – raviraja May 30 '17 at 12:08
  • Possible duplicate of [how to stop browser back button using javascript](https://stackoverflow.com/questions/12381563/how-to-stop-browser-back-button-using-javascript) – Dominik May 30 '17 at 21:41

0 Answers0