Is it possible to use window.history.back()
to navigate back to a particular page? Like an index.php page? I ask because the way my setup is, some users may have to be directed back 2 pages while others might have to only be directed back one page (depending on more complicated things), but they will all have to be directed back to the same index.php page. So, is this possible?

- 31
- 1
- 6
-
Why can't you maintain state (say with a cookie) and simply redirect all users to index.php? – A T - student Jun 27 '14 at 00:36
-
I'm not 100% sure about this, but I believe for security reasons modern browsers no longer allow you to inspect the history. You used to be able to do that, but that leaves open the ability to snoop your history and all the nastiness that comes with that. – CodingGorilla Jun 27 '14 at 00:36
-
@jstrat6, why not use `location='index.php';` directly instead? – Jun 27 '14 at 00:51
-
@AriTrachtenberg could you go a little more in-depth with that? Sounds interesting – jstrat6 Jun 27 '14 at 00:59
-
Well @Arvind the `window.history.back()` allows you to go back to 'exact' spot of the previous page that you left, whereas `location:index.php` would just take me to the top of the index.php page. I want to work it so that users can go back to the post on index.php that they left off on, instead of going back to the top of index.php and having to navigate 'all' the way back down to the post they left off on. – jstrat6 Jun 27 '14 at 01:03
-
1@jstrat6, I suggest `location='index.php'` because your question didn't address about the page state while redirecting back to the same page. Anyways I agree with the suggestion given by AriTrachtenberg, also I would suggest using session to maintain the page state, so later when you rediect the user to the page, using server scripting you can still re-populate the page. – Jun 27 '14 at 01:21
2 Answers
window.history.go();
let you specify how many pages you want to go back or forward in the browser session history.
https://developer.mozilla.org/en-US/docs/Web/API/History#Methods
If you keep a breadcrumb trail, you'll know the correct number needed… although there might be a better way to do this.

- 313
- 1
- 10
-
more clarification: so if users post a comment on this page (the page that i'm trying to go back to index.php from) then I need to navigate back 2 pages, however, if they don't post a comment, I only need to navigate one page? Is this at all possible? Or should I go another route? – jstrat6 Jun 27 '14 at 01:16
-
navigate back 2 pages: `window.history.go(-2);`, navigate one page: `window.history.go(-1);`. So, if you know wether the user has posted or not, I'd say yes. – pandark Jun 27 '14 at 01:32
It turns out to be that in chrome particularly in version 105.0.5195.127 the browser appends previous page's url, aka 'https://yourdomain.com/foo/bar' instead of 'https://yourdomain.com/bar' into your next page while pushing state as if
window.history.pushState((null, null, '/bar')
And I suggest to include the full url in order to handle any url case
export async function pushHistoryState(url)
{
try {
console.log('History state URL:' + url);
let loc = `${location.protocol}//${location.host}/`;
window.history.pushState({ prevUrl: window.location.href }, null, loc + url);
} catch(e) {
console.log(e);
}
}
window.history.state.prevUrl now contains previous URL
useful links: https://stackoverflow.com/a/56184390/6897369 Back button / backspace does not work with window.history.pushState How to get the previous URL in JavaScript? Why does my history.pushState call result in a null state? How to add or append new stateObject to history window.history.pushState change url without removing last part window.history.pushState not working for a second time

- 381
- 1
- 7
- 15