24

Possible Duplicate:
Page history - back button exists?

I need to have a link in some pages of a website, by clicking on it, it needs to go back to the previous page, just like the browser's back button. What is the right way to do that? I'm thinking that I should use some client side scripting like Javascript, right?

Community
  • 1
  • 1
mahsa.teimourikia
  • 1,664
  • 9
  • 32
  • 64

7 Answers7

52

In JavaScript, it's:

history.go(-1);

or

history.back();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
9

Just use

history.go(-1);

But you cannot go forward anymore after that (so the Forward button doesn't bring you back to where you were -- i.e., this is not 100% the same as clicking the Back button).

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 7
    In order to got forward you have to use `back()` instead of `go(-1)`. After a call to `back()` you can use `forward()`. – KeyNone Nov 21 '12 at 09:52
8

You can use:

<form>
    <input type="button" value="Zurück" name="back" onClick="javascript:history.back(1)">
</form>

Or within a link:

<a href="javascript:history.back(1)">back</a> 

If you want to go forward afterwards, use history.forward(1), but be aware: this only works if you called back() before!

KeyNone
  • 8,745
  • 4
  • 34
  • 51
3

Something like:

<script>
function goBack()
{
  window.history.back()
}
</script>

should do it.

mcalex
  • 6,628
  • 5
  • 50
  • 80
3

I think you can use the History API for browsers.

window.history.back()

This should work.

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
3

Clicking on button takes browser back

<INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.back()">
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
1
<FORM><INPUT TYPE="BUTTON" VALUE="Go Back" 
ONCLICK="history.go(-1)"></FORM>

Try this code

Rithu Psks
  • 92
  • 11