303

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"/>
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Doc Holiday
  • 9,448
  • 30
  • 95
  • 150

13 Answers13

516

Add this in your input element

<input
    action="action"
    onclick="window.history.go(-1); return false;"
    type="submit"
    value="Cancel"
/>
kartikluke
  • 2,291
  • 1
  • 18
  • 32
rogerlsmith
  • 6,432
  • 2
  • 19
  • 25
  • 57
    This doesn't work in all browsers for me, I had to do the following `` This answer is quite old, so it could have been an issue introduced into more modern versions of browsers. :) – ctrlplusb May 29 '14 at 10:49
  • 5
    Does this show the previous page from cache or reloads the previous page from the server? – Adrien Apr 28 '16 at 07:57
  • 8
    What's the `action="action"` part for? And is it valid html?? – ban-geoengineering Apr 26 '17 at 18:34
  • @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). – ToolmakerSteve Apr 30 '19 at 14:42
  • Note 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 – Theophany Apr 08 '20 at 12:25
  • 1
    Any idea why this not always working from the first time? It doesn't seem that [this question](https://stackoverflow.com/questions/20034274/duplicate-history-enteries-using-history-pushstate-in-chrome-safari) was answered so far. – Halfist May 18 '20 at 12:59
142
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"/>
ˈvɔlə
  • 7,697
  • 8
  • 56
  • 77
Vadim
  • 4,964
  • 2
  • 19
  • 18
110

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.......
Rizwan Gill
  • 2,133
  • 1
  • 16
  • 26
  • 2
    @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. – Jeromy French Apr 05 '16 at 19:04
16

Simple. One line.

<button onclick="javascript:window.history.back();">Go Back</button>

Like Wim's and Malik's answer, but just in one line.

Andre Mesquita
  • 779
  • 10
  • 22
  • Works 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... – Marek Bernád Dec 03 '19 at 06:59
16
<input name="action" type="submit" value="Cancel" onclick="window.history.back();"/> 
hspain
  • 17,488
  • 5
  • 18
  • 31
14

window.history.back();

<button onclick="goBack()">Go Back</button>

<script>
function goBack() {
    window.history.back();
}
</script>
Malik Khalil
  • 5,827
  • 2
  • 37
  • 32
14

Works for me everytime

<a href="javascript:history.go(-1)">
    <button type="button">
        Back
    </button>
</a>
CroMagnon
  • 1,206
  • 7
  • 20
  • 32
Mannu saraswat
  • 995
  • 5
  • 13
14

Shortest Yet!

<button onclick="history.go(-1);">Go back</button>

http://jsfiddle.net/qXrbx/

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 tag for a back button seems more appropriate than tags with names and types...

Michael Durrant
  • 89,394
  • 88
  • 305
  • 468
12

You just need to call the following:

history.go(-1);
Rich O'Kelly
  • 40,274
  • 9
  • 81
  • 111
5

This is the only thing that works on all current browsers:

<script>
function goBack() {
    history.go(-1);
}
</script>
<button onclick="goBack()">Go Back</button>
Wim Mostrey
  • 267
  • 5
  • 8
3

Access window.history and then call back()

window.history.back()
Gaurav Patil
  • 734
  • 8
  • 19
0

the only one that worked for me:

function goBackAndRefresh() {
  window.history.go(-1);
  setTimeout(() => {
    location.reload();
  }, 0);
}
DevTheJo
  • 1,959
  • 2
  • 19
  • 21
-1

100% work

<a onclick="window.history.go(-1); return false;" style="cursor: pointer;">Go Back</a>
Fezal halai
  • 663
  • 7
  • 11