Onclick javascript to make browser go back to previous page?
581,081
Solution 1
Add this in your input element
<input
action="action"
onclick="window.history.go(-1); return false;"
type="submit"
value="Cancel"
/>
Solution 2
history.back()
or
history.go(-1)
Put this to the button onclick
handle. It should look like this:
<input name="action" onclick="history.back()" type="submit" value="Cancel"/>
Solution 3
For Going to previous page
First Method
<a href="javascript: history.go(-1)">Go Back</a>
Second Method
<a href="##" onClick="history.go(-1); return false;">Go back</a>
if we want to more than one step back then increase
For going 2 steps back history.go(-2)
For going 3 steps back history.go(-3)
For going 4 steps back history.go(-4)
and so on.......
Solution 4
<input name="action" type="submit" value="Cancel" onclick="window.history.back();"/>
Solution 5
Simple. One line.
<button onclick="javascript:window.history.back();">Go Back</button>
Like Wim's and Malik's answer, but just in one line.
Related videos on Youtube
Author by
Doc Holiday
I work as a Java Developer who often has to do SQL development, HQL development, and front-end styling using various JS frameworks.
Updated on August 25, 2021Comments
-
Doc Holiday over 2 years
Is there a function I can attach as a click event of a button to make the browser go back to previous page?
<input name="action" type="submit" value="Cancel"/>
-
John Strickler over 12 yearswindow.history.back()
-
-
Vadim over 12 yearsPut this to the button onClick handle
-
ctrlplusb almost 10 yearsThis doesn't work in all browsers for me, I had to do the following
<input action="action" type="button" value="Back" onclick="window.history.go(-1); return false;" />
This answer is quite old, so it could have been an issue introduced into more modern versions of browsers. :) -
Jeromy French about 8 years@JeromeJ it is a link that does nothing if JavaScript isn't enabled/working (for whatever reason) on the page. If JavaScript is working, it takes the browser back one page.
-
Adrien about 8 yearsDoes this show the previous page from cache or reloads the previous page from the server?
-
WowThatsLoud almost 8 yearsYou could be more clear like giving the answer in the code
-
Costa Michailidis about 7 yearsWhat's the browser support on that?
-
ban-geoengineering about 7 yearsWhat's the
action="action"
part for? And is it valid html?? -
ToolmakerSteve about 5 years@Adrien - its javascript, so it is running in the browser. The browser should reuse cached page (assuming browser settings and page http header settings permit doing so).
-
Marek Bernád over 4 yearsWorks fine, but oftentimes if user decides to change language on specific page with the button like this, the back button to previous page will turn the language back, instead of page...
-
CheddarLizzard about 4 yearsNote that inline JS should not be used in production because: 1. JS code should be in dedicated and minified
.js
files 2. inline JS should be disabled through a Content Security Policy to mitigate XSS injections -
Halfist almost 4 yearsAny idea why this not always working from the first time? It doesn't seem that this question was answered so far.
-
Diego Fortes almost 3 years@Costa pretty good, unless you're developing to Opera Mini.
-
Josh McGee over 2 yearssome of the others didn't take me to the previous page if I had reloaded, but this one does