14

Is it possible to tell window.location.href to go back 2 pages in history and reload the page that is called?

i only managed to get it to work like this:

<script type="text/javascript">
window.location.href = "http://www.google.com/";
</script>

Everything else i try with the command history.back(-2) does not work in my case.

Jazi
  • 6,569
  • 13
  • 60
  • 92
Marcel Wasilewski
  • 2,519
  • 1
  • 17
  • 34

3 Answers3

25

Try this, it will take you two step back

 history.go(-2);

eg,

<a href="www.mypage.com" onclick="javascript:history.go(-2)"> Link </a>
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
15

You should use history.back(2);, not history.back(-2);.

Jazi
  • 6,569
  • 13
  • 60
  • 92
  • 4
    `history.back(2)` does not work. `history.go(-2)` is the correct answer. check https://developer.mozilla.org/en-US/docs/Web/API/History – Isaiahiroko Apr 30 '19 at 07:03
2

you can use function history.go(-2) to move backward two pages previously and history.go(2) to move forward 2 page that you left after you back from 2 pages before

<script>
function goBack() {
  window.history.go(-2);
}
</script>
bayu
  • 61
  • 1
  • 11