0

I want to make a goBack button with javascript that goes back to when the original post was viewed.

Eg. I have /something/ this is the original URL then I have /something/1/ /something/2/ /something/3/ /something/4/

Is it possible to make window.history.back(); go back before viewing page /something/? Like I started on page something I viewed page 2 and 4 then I clicked to close and takes me back to one page before something.

Also my page numbers are page breaks thats why I want to go back before the something page was viewed.

Thank you for the help. Sorry my English isn't the best to explain what I mean.

PChristina
  • 11
  • 1
  • 1
    What do you mean by "Is it possible to make the window.history.back(); go back before enter /something/ if /something/x/ was viewed?" – Hello Universe May 25 '18 at 12:05
  • `window.location.href="/something/"` instead – Justinas May 25 '18 at 12:06
  • Do you mean: I was at /otherthing, then went to /something, then went to /something/x, now i want to go back to /otherthing ? – Shilly May 25 '18 at 12:06
  • 1
    Possible duplicate of [How do I use window.history in JavaScript?](https://stackoverflow.com/questions/31528432/how-do-i-use-window-history-in-javascript) – Kaddath May 25 '18 at 12:12
  • 1
    Easiest way would be to just save the url somewhere and just route to that url again instead of using the back button. If it has to be the back button, you could try manipulating the history so the /something pages don't show up and the last stored page was /otherthing. – Shilly May 25 '18 at 12:14

1 Answers1

0
function goBack() {
window.history.go(-2); 
}

that way you can go back 2 pages. In your case you can go to the main category from the subcategory. I think that is what you mean with the question.

eboycina
  • 94
  • 2
  • 12
  • Although I didn't vote, the problem is that it's not always going to be exactly two pages. – Shilly May 25 '18 at 12:11
  • 1
    yes, but then you just add that if you are on a subcategory it goes back 2 if not it goes back only 1... or maybe I just don't understand what the question was in the first place :( – eboycina May 25 '18 at 12:12
  • The question itself is quite confusing, so I think its dangerous to post a answer for it. Be careful in these cases, I suggest posting your answers as comments when you don't know if its the right answer. (it wasn't me downvoting though) – Blind Roach May 25 '18 at 12:16
  • Thank you very much but that's not what I want. I knew that already. If I saw all 9 pages it only gets me back 2 at a time. – PChristina May 25 '18 at 12:29
  • a ok.. take a look at this link: https://www.w3schools.com/js/js_window_location.asp you can use "window.location.href" and set it to /something, that way every click the button, no matter where you are will take you to the page you want – eboycina May 25 '18 at 12:35