history.replaceState still adds entries to the "Browsing History"
Asked Answered
Q

2

28

Specifically, calling the following snippet of code:

history.replaceState(undefined, undefined, "#" + value)

will correctly not affect the back button behavior of the current page, but will add an entry to the "browsing history" page, which I don’t want. Pictured below is Chrome’s History page, but this also happens in Firefox.

Is there some way to replace the current URL without adding an entry to the user’s browsing history?

I’m inquiring about the standalone Browser History page, not the actual page navigation history that the Back button uses.

Chrome Browser History

Quentin answered 7/11, 2014 at 2:37 Comment(6)
I've wondered about this also but have never found an answer.Thoth
I don't think what you're asking is possible with the history api, if at all. "history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one." developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/…Carrageen
@realseanp Right, and the OP's screenshot shows that replaceState is creating new distinct entries. In current Chrome Canary, with history and cache purged, I am still getting similar behavior. The back button steps through the "replaced" states.Elva
This feature request has been open for 8 years on bugzilla - probably nothing will change any time soon: bugzilla.mozilla.org/show_bug.cgi?id=753264Plica
@AnilRedshift If you make an answer out of your comment, I could give you the bounty. (If I'm online in the next 23 hours.)Mopup
Sure, thanks @MopupPlica
P
4

I can't find any W3C spec on the global history, but even the session history is left unspecified to what the expected behavior is.

In practice, this has been an open bug for over 8 years with most of the browsers choosing to implement global history this way. In short, there probably isn't a workaround other than not calling pushState/replaceState.

Plica answered 23/12, 2019 at 5:52 Comment(0)
G
0

Use location.replace("#" + value) instead. Like history.replaceState(...) it will not add new back/forward browser history entries, but will also not add entries to Chrome's "Browser History" page (i.e. session history).

See also https://mcmap.net/q/195417/-can-you-use-hash-navigation-without-affecting-history

Glabella answered 24/3, 2023 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.