Onclick javascript to make browser go back to previous page?
JavascriptHtmlBrowserOnclickDom EventsJavascript Problem Overview
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"/>
Javascript Solutions
Solution 1 - Javascript
Add this in your input element
<input
action="action"
onclick="window.history.go(-1); return false;"
type="submit"
value="Cancel"
/>
Solution 2 - Javascript
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 - Javascript
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 - Javascript
<input name="action" type="submit" value="Cancel" onclick="window.history.back();"/>
Solution 5 - Javascript
Simple. One line.
<button onclick="javascript:window.history.back();">Go Back</button>
Like Wim's and Malik's answer, but just in one line.
Solution 6 - Javascript
Shortest Yet!
<button onclick="history.go(-1);">Go back</button>
I prefer the .go(-number)
method as then, for 1 or many 'backs' there's only 1 method to use/remember/update/search for, etc.
Also, using a
Solution 7 - Javascript
> window.history.back();
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
Solution 8 - Javascript
Works for me everytime
<a href="javascript:history.go(-1)">
<button type="button">
Back
</button>
</a>
Solution 9 - Javascript
You just need to call the following:
history.go(-1);
Solution 10 - Javascript
This is the only thing that works on all current browsers:
<script>
function goBack() {
history.go(-1);
}
</script>
<button onclick="goBack()">Go Back</button>
Solution 11 - Javascript
Access window.history
and then call back()
window.history.back()
Solution 12 - Javascript
the only one that worked for me:
function goBackAndRefresh() {
window.history.go(-1);
setTimeout(() => {
location.reload();
}, 0);
}
Solution 13 - Javascript
100% work
<a onclick="window.history.go(-1); return false;" style="cursor: pointer;">Go Back</a>