Why doesn't changing the hash affect the length of the story?

Consider this snippet:

console.log( "1st", history.length ); location.hash = location.hash + "some-value"; console.log( "2nd", history.length ); setTimeout( function() { console.log( "3rd", history.length ); history.back(); console.log( "4th", history.length ); }, 1000 ); 

https://jsfiddle.net/1kqLofq4/2/

I am wondering why changing the hash does not adjust the length of the history, but is using history.back () required to return to changing the hash? I tested this scenario with Firefox 46 and Chrome 49. The output is always similar to this:

 1st 17 2nd 17 3rd 17 4th 17 

I tried to find some specifications or information about this case, why it could be the intended behavior and how I could detect changes in the history, like this, using some other information than history.length. But all I got was a hint of using some fancy framework plugins that definitely don't interest me.

+6
source share
1 answer

I may be wrong, but the specification does not seem to support this behavior. It could be a mistake.

From step 6 to section 7.8.1: Navigating the WHATWG HTML specification documents.

  1. Fragments: if it’s not an overloaded download: apply the URL parser algorithm to the absolute URL of the resource and the address of the active document browsingContext; if all components of the resulting URL entry, ignoring any fragment components, are identical, and the resource must be selected using GET , and the resource URL entry contains a fragment component that is not null (even if it is empty), then go to to this fragment and abort these steps.

From section 7.8.9 Going to fragment :

  1. Add a new record at the end of the History object, representing the new resource, as well as its Document object, the associated state and the current history scroll restore setting. Its URL should be set to the address to which the navigator used. The title should be left unchanged.

I have not seen anything about replacing a history record in any way instead of changing it, if it is just a change to a fragment. The behavior of .length and .back() should refer to the same set of history records (the combined β€œshared session history”), so it seems that they should not be inconsistent in this way. From the notes in this section:

window.history.length

Returns the number of entries in a shared session history.

window.history.back()

Returns one step in the history of a joint session.

+1
source

All Articles