65

Possible Duplicate:
Go Back to Previous Page
get back to previous page

How to get the previous page in javascript coding. Go to the previous page when click that back button.

Community
  • 1
  • 1
Rithu
  • 1,289
  • 3
  • 19
  • 38
  • My inclination is basically _don't._ The browser already has a back button, so adding a button to your page is redundant, and also if the user opened your page in a new tab or new window then you _can't_ go back. – nnnnnn Nov 22 '12 at 11:25
  • 1
    @nnnnnn is basically correct, but there may be some situations where it may be easier to implement a back button (e.g. if the browser is running in full-screen, such as a kiosk). – FixMaker Nov 22 '12 at 11:27
  • possible duplicate of [get back to previous page](http://stackoverflow.com/questions/2968425/get-back-to-previous-page) and [Go Back to Previous Page](http://stackoverflow.com/questions/2548566/go-back-to-previous-page) – Felix Kling Nov 22 '12 at 11:29

4 Answers4

88

Here is the code

<input type="button" value="Back" onclick="window.history.back()" /> 
Ramya
  • 1,344
  • 1
  • 8
  • 18
49

You can either use:

<button onclick="window.history.back()">Back</button>

or..

<button onclick="window.history.go(-1)">Back</button>

The difference, of course, is back() only goes back 1 page but go() goes back/forward the number of pages you pass as a parameter, relative to your current page.

George
  • 36,413
  • 9
  • 66
  • 103
26

There's a few ways, this is one:

window.history.go(-1);
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 2
    This is great because you can also use `window.history.go(-2);` (which I just needed) or `window.history.go(-4);`, etc. – cssyphus Nov 18 '17 at 16:55
8

onclick="history.go(-1)" Simply

vusan
  • 5,221
  • 4
  • 46
  • 81