13

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,344
  • 12
  • 57
  • 89
Marcel Wasilewski
  • 2,404
  • 1
  • 15
  • 32

3 Answers3

24

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,391
  • 1
  • 20
  • 44
14

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

Jazi
  • 6,344
  • 12
  • 57
  • 89
  • 3
    `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
1

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
  • 51
  • 1
  • 10