How to usehistory.back() Method in Javascript

The back() method of the window.history object is used to go back to the previous page in the current session history. In case there is no previous page, this method does not call anything. The onclick event can be specified with this method to go back one page in history. 

Syntax:

history.back();

Example: This example uses history.back() method to redirect the browser to the previous page. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM History.back() Method</title>
    <style>
        h1 {
            color: green;
        }
 
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>w3wiki</h1>
    <h2>DOM History.back() Method</h2>
    <p>For going to the previous URL in the history,
        double-click the "Go back" button: </p>
    <button onClick="history_back()">Go back</button>
    <script>
        function history_back() {
            window.history.back();
        }
    </script>
</body>
 
</html>


Output:

Output

How to redirect browser window back using JavaScript ?

In this article, we will redirect the browser window back using JavaScript.

There are two approaches used to redirect the browser window back:

Table of Content

  • Using history.back() Method
  • Using history.go() Method

Similar Reads

Approach 1: Using history.back() Method

The back() method of the window.history object is used to go back to the previous page in the current session history. In case there is no previous page, this method does not call anything. The onclick event can be specified with this method to go back one page in history....

Approach 2: Using history.go() Method

...