Home > Back-end >  window.history.back() won't take me back to homepage
window.history.back() won't take me back to homepage

Time:05-31

I'm creating a website right now with multiple pages, that you access through the homepage however, each page has a back button that takes you back to the previous page, however the back button doesn't take me back to the homepage and will instead take me back to the page before that. I noticed that if I click once into a separate link from the homepage that new link will overwrite the homepage from my browser history. What is the explanation for this and what is the fix?

CodePudding user response:

To go back to the home page, you can try the code below:

location.href = "/";

CodePudding user response:

Tha is the expected behaviour as per MDN

The History.back() method causes the browser to move back one page in the session history. It has the same effect as calling history.go(-1). If there is no previous page, this method call does nothing.

To go back to the homepage, you can provide a static link to your website homepage, similar to www.example.com

  • Related