Open In App

How to access history in JavaScript ?

Last Updated : 25 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to access history in JavaScript. We will use the History object to access the history stack in JavaScript. Every web browser will store the data on which websites or webpages opened during the session in a history stack. To access this history stack we need to use the History object in JavaScript.

History object: The history object contains the browser’s history. The URLs of pages visited by the user are stored as a stack in the history object. There are multiple methods to manage/access the history object.

Methods of History object:

1. The forward() Method: This method is used to load the next URL in the history list. This has the exact functionality as the next button in the browser. There are no parameters and it will return nothing.

Syntax:

history.forward()

2. The back() Method: This method is used to load the previous URL in the history list. This has the exact functionality as the back button in the browser. There are no parameters and it will return nothing.

Syntax:

history.back()

3. The go() Method: This method is used to loads a URL from the history list.

Syntax:

history.go(integer)

Parameters: This method has a single parameter that specifies the URL from the history. It can have the following values:

Value Usage
-1 Loads the previous page from the history stack
0 Reloads the page.
1 Loads the next page from the history stack

Example 1: Using forward() and back() 

geekshtml.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 1</h1>
  
    <a href="/geekshtml2.html">Go to page 2</a> <br>
  
    back button : <input type="button" 
        value="Back" onclick="previousPage()"> <br>
  
    forward button : <input type="button" 
        value="Forward" onclick="NextPage()"> <br>
          
    <script>
        function NextPage() {
            window.history.forward()
        }
        function previousPage() {
            window.history.back();
        }
    </script>
</body>
  
</html>


geekshtml2.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 2</h1>
  
    back button : <input type="button" 
        value="Back" onclick="previousPage()"> <br>
  
    forward button : <input type="button" 
        value="Forward" onclick="NextPage()"> <br>
          
    <script>
        function NextPage() {
            window.history.forward()
        }
        function previousPage() {
            window.history.back();
        }
    </script>
</body>
  
</html>


Output:

Example 2: Using go(), forward() and back() methods.

geekshtml.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 1</h1>
  
    <a href="/geekshtml2.html">Go to page 2</a> <br>
  
    back button : <input type="button" 
        value="Back" onclick="previousPage()"> <br>
  
    forward button : <input type="button" 
        value="Forward" onclick="NextPage()"> <br>
  
    Go button : <input type="button"
        value="go" onclick="go()"> <br>
          
    <script>
        function NextPage() {
            window.history.forward()
        }
        function previousPage() {
            window.history.back();
        }
        function go() {
            window.history.go(0);
        }
    </script>
</body>
  
</html>


geekshtml2.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 2</h1>
  
    back button : <input type="button" 
        value="Back" onclick="previousPage()"> <br>
  
    forward button : <input type="button"
        value="Forward" onclick="NextPage()"> <br>
  
    Go button : <input type="button" 
        value="go" onclick="go()"> <br>
          
    <script>
        function NextPage() {
            window.history.forward()
        }
        function previousPage() {
            window.history.back();
        }
        function go() {
            window.history.go(0);
        }
    </script>
</body>
  
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads