20

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,594
  • 9
  • 29
  • 63

7 Answers7

49

In JavaScript, it's:

history.go(-1);

or

history.back();
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
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
  • 31,619
  • 8
  • 58
  • 72
  • 6
    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
7

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,147
  • 4
  • 33
  • 50
3

Something like:

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

should do it.

mcalex
  • 6,418
  • 5
  • 45
  • 75
3

Clicking on button takes browser back

<INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.back()">
Mudassir Hasan
  • 26,910
  • 19
  • 95
  • 126
2

I think you can use the History API for browsers.

window.history.back()

This should work.

Amogh Talpallikar
  • 11,904
  • 13
  • 78
  • 134
1
<FORM><INPUT TYPE="BUTTON" VALUE="Go Back" 
ONCLICK="history.go(-1)"></FORM>

Try this code

Rithu Psks
  • 92
  • 11