What is the role of the window.history object in JavaScript?


The window.history object is used to go back or to the next page of the web browser. It has the browser's history and comes with the following two methods −

  • history.back − Go to previous URL
  • history.next − Go to next URL

Example

You can try to run the following code to learn how to work with window.history object in JavaScript −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         function backPage() {
            window.history.back()
         }
         function nextPage() {
            window.history.next()
         }
      </script>
      <input type="button" value="Previous URL" onclick="backPage()">
      <input type="button" value="Next URL" onclick="nextPage()">
   </body>
</html>

Updated on: 23-Jun-2020

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements