Open In App

How to make browser to go back to previous page using JavaScript ?

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

There are two popular methods to navigate a browser back to the previous page. These methods allow easy navigation in a web session's history, offering simple and effective ways to move backward or forward through a user's browsing history.

These are the following two ways:

Method 1: Using history.go() Method

The history.go() method, is part of the window.history object allows navigation through a session's history by specifying a delta value. This value represents the number of steps to move forward or backward in history:

  • A positive delta moves forward in history.
  • A negative delta moves backward in history.

To go back to the previous page, you can pass -1 as the delta value, which moves the browser back one step in history.

Syntax:

window.history.go(-1);

Note: If we want to go back more than one step then increase the value of delta from '-1' to as much as you want.

Example: This example shows the use of history.go() method for going back to the previous page of the browser.

Page 1
<!DOCTYPE html>
<html>

<head>
    <title>
        How to make browser to go back to
        previous page using JavaScript ?
    </title>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 1</h2>

    <p>
        Click on the link to get
        to the second page.
    </p>

    <a href="page2.html">Go to Page 2</a>
</body>

</html>
Page 2
<!DOCTYPE html>
<html>

<head>
    <title>
        How to make browser to go back to
        previous page using JavaScript ?
    </title>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 2</h2>

    <p>
        Click on the button to go back
        to the previous page.
    </p>

    <button onclick="history.go(-1)">
        Click here to go back
    </button>
</body>

</html>

Output:

history
Going back to previous page by using history.go() method

Method 2: Using the history.back() Method

The history.back() method is a simpler and more specific way to navigate backward by one step in the session's history. This method automatically takes the browser back to the previous page, if available.

Syntax:

window.history.back();

Example: This example shows the use of history.back() method for going back to the previous page of the browser.

Page 1
<!DOCTYPE html>
<html>

<head>
    <title>
        Onclick javascript to make browser go
        back to previous page?
    </title>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 1</h2>

    <p>
        Click on the link to get
        to the second page.
    </p>

    <a href="page2.html">Go to Page 2</a>
</body>

</html>
Page 2
<!DOCTYPE html>
<html>

<head>
    <title>
        Onclick javascript to make browser
        go back to previous page?
    </title>
</head>

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>

    <b>
        Onclick javascript to make browser
        go back to previous page?
    </b>

    <h2>Page 2</h2>

    <p>
        Click on the button to go
        back to the previous page.
    </p>

    <button onclick="history.back()">
        Click here to go back
    </button>
</body>

</html>

Output:

history
Going back to previous page by using history.back() method

Next Article

Similar Reads