Open In App

How to access history in JavaScript ?

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
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:

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

Example 1: Using forward() and back() 

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>
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:

file

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

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>
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:

file


Next Article

Similar Reads