4

I'm creating an image slider in css3 with the :target selector. I have a next and previous button that are executing this line of javascript

window.location = "#image" + image;

The problem is that if i click the back button of my browser, it shows the previous image of the slider. I want the back button to go to the previous page, not the previous anchor.

Thanks for the help

2 Answers2

4

Use History API:

history.replaceState({}, document.title, "#image" + image);

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.

replaceState() is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
4
window.location.replace("#image" + image);

This does a simple redirect without adding it to the history.
MDN: https://developer.mozilla.org/en-US/docs/Web/API/Location.replace
Previous question: What's the difference between window.location= and window.location.replace()?

Community
  • 1
  • 1
17xande
  • 2,430
  • 1
  • 24
  • 33