最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to not keep history of html anchor - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Oct 11, 2013 at 14:28 Marc-André TherrienMarc-André Therrien 5495 silver badges25 bronze badges 3
  • soooo, why exactly is it you're changing the hashtag of the URL if you don't need the hashtag history? – Zathrus Writer Commented Oct 11, 2013 at 14:33
  • 4 well the most simple thing would be to stop changing the url, and find another way to slide the images – Bobby5193 Commented Oct 11, 2013 at 14:33
  • I agree with that, i will probably change it to a jquery slider instead of css3 – Marc-André Therrien Commented Oct 11, 2013 at 15:35
Add a ment  | 

2 Answers 2

Reset to default 4
window.location.replace("#image" + image);

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

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.

发布评论

评论列表(0)

  1. 暂无评论