gpt4 book ai didi

javascript - 为什么更改哈希不会影响历史长度?

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:22 24 4
gpt4 key购买 nike

考虑这个片段:

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/

我很好奇为什么更改哈希不会调整历史长度,但需要使用 history.back() 来恢复哈希更改?我已经用 Firefox 46 和 Chrome 49 测试了这个场景。输出总是类似于这样:

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

我已经尝试搜索有关此案例的一些规范或信息,为什么这可能是预期的行为,以及我如何使用 history.length 以外的其他信息来检测这样的历史记录变化。但我得到的只是关于使用一些我绝对不感兴趣的花哨框架插件的提示。

最佳答案

我可能有误解,但规范似乎不支持这种行为。这可能是一个错误。

来自 step 6在 WHATWG HTML 规范的7.8.1:跨文档导航部分。

  1. Fragments: If this is not a reload-triggered navigation: apply the URL parser algorithm to the absolute URL of resource and the address of the active document of browsingContext; if all the components of the resulting URL record, ignoring any fragment components, are identical, and resource is to be fetched using GET, and the URL record of resource has a fragment component that is not null (even if it is empty), then navigate to that fragment and abort these steps.

来自 section 7.8.9 Navigating to a fragment :

  1. Append a new entry at the end of the History object representing the new resource and its Document object, related state, and current history scroll restoration preference. Its URL must be set to the address to which the user agent was navigating. The title must be left unset.

我没有看到任何有关以某种方式替换历史条目而不是修改它的内容,如果它只是一个片段更改。 .length.back() 的行为都应该引用同一组历史记录条目(组合的“联合 session 历史记录”),所以看起来他们不应该表现出这样的不一致。来自本节的注释:

window.history.length

Returns the number of entries in the joint session history.

window.history.back()

Goes back one step in the joint session history.

关于javascript - 为什么更改哈希不会影响历史长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com