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

There is two popular way to make browsers go back to the previous page by clicking JavaScript event, both methods are described below:

Method 1: Using history.go() method: The go() method of the window.history object is used to load a page from the session history. It can be used to move forward or backward using the value of the delta parameter. A positive delta parameter means that the page would go forward in history. Similarly, a negative delta value would make the page go back to the previous page.
This method can be used with ‘-1’ as the delta value to go back one page in history. The onclick event can be specified with the method to go back one page 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:

Page 1

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Page 2

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:

  • Before clicking on the link:
    ex1-page-1
  • After clicking on the link:
    ex1-page-2
  • After clicking on the button:
    ex1-page-1

Method 2: Using the 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 call does nothing.
The onclick event can be specified with this method to go back one page in history.

Syntax:

window.history.back()

Example:

Page 1

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Page 2

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output:

  • Before clicking on the link:
    ex2-page-1
  • After clicking on the link:
    ex2-page-2
  • After clicking on the button:
    ex2-page-1


My Personal Notes arrow_drop_up

Maybe this silence is dangerous

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.