How to use the history.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 call does nothing. The onclick event can be specified with this method to go back one page in history.
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" > w3wiki </ 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" > w3wiki </ 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:
How to make browser to go back to previous page using JavaScript ?
There are two popular ways to make browsers go back to the previous page by clicking the JavaScript event.
These are the following two ways:
Table of Content
- Using history.go() method
- Using the history.back() method
Contact Us