Saturday, September 6, 2025
HomeLanguagesJavascriptHow to access history in JavaScript ?

How to access history in JavaScript ?

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() 

neveropenhtml.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 1</h1>
  
    <a href="/neveropenhtml2.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>


neveropenhtml2.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.

neveropenhtml.html




<!DOCTYPE html>
<html>
  
<head>
    <style>
        a,
        input {
            margin: 10px;
        }
    </style>
</head>
  
<body>
    <h1>This is page 1</h1>
  
    <a href="/neveropenhtml2.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>


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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS