Search code examples
javascripthtmlbrowser-history

Use Javascript history.go() to the real "previous" page


Assuming we have a history like this:

 - Home
 - Page 1
 - Page 2

JS history.go(-1) works well to go to Page 1... but what if our current history is

 - Home
 - Page 1
 - Page 2
 - Page 2 (2nd time)
 - Page 2 (3rd time)
 - ...
 - Page 2 (nth time)

How can we use JavaScript history object to go back to Page 1?


Solution

  • The signature for this method is history.go(number|URL) from (w3school). So you can use a string parameter instead of number, and if (in some manner) you know which was the url for page2 you can use it.

    history.go("http://localhost/page2");