5

I have the following code that attempts to manipulate the window.history stack:

  console.log("before:" + window.history.length);
  window.history.pushState(null, null, '/page1');
  console.log("after:" + window.history.length);

This always prints the same in the before and after, even though I'm adding a state:

before:50
after:50

Why is pushState not incrementing the history length? By the way, length is always 50 even though I'm refreshing the page.

ps0604
  • 1,227
  • 23
  • 133
  • 330
  • Works as it should for me: https://jsfiddle.net/0qrgesa3/ – StudioTime Oct 14 '18 at 14:32
  • Thanks, I'm using Angular, it may be an Angular issue – ps0604 Oct 14 '18 at 14:38
  • This may vary by user agent. Some have a cap on the number of elements in the history. From [the standard](https://www.w3.org/TR/html50/browsers.html#dom-history-pushstate): "User agents may limit the number of state objects added to the session history per page." – Heretic Monkey Oct 14 '18 at 14:39
  • Possible duplicate of [window.history.length maximum value](https://stackoverflow.com/questions/16355549/window-history-length-maximum-value) – Heretic Monkey Oct 14 '18 at 14:41
  • Firefox (maybe other browsers) impose a limit on the maximum value of `history.length`. By default in Firefox it's 50. What the semantics are of doing a `.pushState()` when the length is 50 is a hard topic to look for, but as far as I can tell it effectively "pushes" entries off the *bottom* of the list, so that there are still 50 entries but the "back" button does what you expect. – Pointy Oct 22 '20 at 19:28

0 Answers0