javascript - Why does the size of my window.history not decrease? - Stack Overflow

When I access the JavaScript variable window.history.length I am returned the size of my window history

When I access the JavaScript variable window.history.length I am returned the size of my window history. As I navigate forward from one page to another and then on to another, the length of window.history increases. As I go back through my history using the browser back button, the value of window.history does not decrease even though I am moving back through the history. Why is this?

When I access the JavaScript variable window.history.length I am returned the size of my window history. As I navigate forward from one page to another and then on to another, the length of window.history increases. As I go back through my history using the browser back button, the value of window.history does not decrease even though I am moving back through the history. Why is this?

Share Improve this question asked Nov 3, 2014 at 3:58 KolbanKolban 15.3k3 gold badges44 silver badges71 bronze badges 1
  • 1 It can decrease if you navigate back a few pages and then visit a different page than you've gone before. Click around a few times on StackOverflow, then check the length. Go back a few pages and check the length again, it should be the same. Click a different length and it will be smaller. – Cory Danielson Commented Nov 3, 2014 at 4:01
Add a ment  | 

1 Answer 1

Reset to default 7

Think of window.history as a logically growing list or array of pages visited. Initially, there is only the initial page. When you navigate forward, a new entry is added to the end of the list and window.history.length increases by one. When you go back a page, the list does not have the entry you were previously at removed. Instead, it remains and you then have the opportunity to go forward (back to the page you just came back from).

What this means is that you shouldn't use the length of window.history as an indicator of how far you are from the start of your browsing session. For example, if your window.history.length has a value of 5 that does not mean that if you go back 3 pages you will be at the 2nd page you visited. window.history.length represents the length of the number of pages you have visited in a forward direction but says nothing about where you currently are within that list.

If after navigating backwards, you visit a forward page that is not the same as the previous one in the forward chain, the list will be changed and so will the length as you are now no longer able to forward visit the entries that you had previously returned from.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745175751a4615183.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信