How to disable browser's back button with JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To disable web browsers’ back button, try to run the following code. This is the code for current HTML page,

Example

<html>
   <head>
      <title>Disable Browser Back Button</title>
      <script src = "http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
   </head>
   
   <body>
      <a href = "newpage.html">Next Page</a>
   </body>
   <script>
      $(document).ready(function() {
         function disablePrev() { window.history.forward() }
         window.onload = disablePrev();
         window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
      });
   </script>
</html>

The following is the code for newpage.html,

<html>
   <body>
      Go to back page using web browser back button.
   </body>
</html>a
raja
Published on 01-Mar-2018 11:04:28
Advertisements