Open In App

Web API History

Web API History is the API that is used to access the browser history. It basically gives us access to the history of global objects. This API is useful when we are dealing with the session history of a web browser. It gives us some useful methods and properties to access and manage the history object of the browser.

Concepts and Usage

Some of the basic usages of History API are:



Web API History Interfaces

Web API History Methods

Example 1: In this example, we will go forward and back using the history forward and back method.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <div>
            <h1 style="color: green;">
                  GeeksforGeeks
              </h1>
            <h2>Hello geeks</h2>
            <h2>This is First Page</h2>
            <button onclick="location.href='b.html'">
                  Go next page
              </button> <br/><br />
            <button onclick="history.forward(1)">
                  Go to Previoous page
              </button>
        </div>
    </center>
</body>
 
</html>




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
 
        <div>
            <h1 style="color: green;">
                  GeeksforGeeks
              </h1>
            <h2>Hello geeks</h2>
            <h2>This is second page</h2>
 
            <button onclick="location.href='c.html'">
                  Go to next Page
              </button>
            <button onclick="history.back()">
                  Go Back
              </button>
            <button onclick="history.forward(1)">
                  Go to Previoous page
              </button>
        </div>
    </center>
</body>
 
</html>




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <div>
            <h1 style="color: green;">
                  GeeksforGeeks
              </h1>
            <h2>Hello geeks</h2>
            <h2>This is Third page</h2>
            <button onclick="history.back()">
                  Go to previous page
              </button>
        </div>
    </center>
</body>
 
</html>

Output:



history1

Example 2: In this example we will move forward and back in session history using history.go() method.




<!-- home.html -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <div>
            <h1 style="color: green;">
                GeeksforGeeks
            </h1>
            <h3>Hello geeks</h3>
            <h3>This is First Page</h3>
            <h3> click on button to visit next site</h3>
            <button onclick="location.href='b.html'">
                Go to Second Page
            </button>
            <button onclick="location.href='c.html'">
                Go to Third Page
            </button>
 
        </div>
    </center>
</body>
 
</html>




<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <div>
            <h1 style="color: green;">
                  GeeksforGeeks
              </h1>
            <h2>Hello geeks</h2>
            <h2>This is second page</h2>
            <button onclick="history.back()">
                  Go Back
              </button>
            <button onclick="location.href='c.html'">
                  Go to next Page
              </button>
 
        </div>
    </center>
</body>
 
</html>




<!-- temp.html -->
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <div>
            <h1 style="color: green;">
                  GeeksforGeeks
              </h1>
            <h2>Hello geeks</h2>
            <h2>This is Third page</h2>
            <button onclick="history.go(-1)">
                  Go to Second Page
              </button>
            <button onclick="history.go(-2)">
                  Go to First Page
              </button>
 
        </div>
    </center>
</body>
 
</html>

Output:

history1

Browser Support:


Article Tags :