gpt4 book ai didi

Javascript 历史记录无法检索 onpopstate 中的值

转载 作者:行者123 更新时间:2023-11-28 21:05:40 27 4
gpt4 key购买 nike

这是我的代码:

function pushState() {

...snip... (SelectedMenuID has a value of 8)

var stateObj = { selectedMenu: SelectedMenuID };
history.pushState(stateObj, "The title to use", path);
}

window.onpopstate = function(event) {
if (event.state != null) {
alert(event.state.selectedMenu);
}
};

几个问题:

  • 点击每个页面可以很好地更新导航栏 URL,但不会按照我的预期将标题更新为要使用的标题
  • 按回它会创建警报,但该值是未定义,而不是我期望的8

最佳答案

  1. 根据https://github.com/browserstate/history.js/wiki/The-State-of-the-HTML5-History-API当前没有浏览器根据 pushState() 中的值更新页面标题。 。我个人认为这是正确的行为 - 页面标题应该反射(reflect) <title>页面头部的元素。

  2. 当您调用pushState()时然后使用后退按钮,您不会返回到最后一个pushState(实际上是当前状态),而是返回到之前的状态。在您的情况下,听起来您正在看到页面加载时的状态,这就是 selectedMenu 未定义的原因。顺便说一句,只有 Chrome 会触发 onpopstate 来匹配初始页面加载,因此在其他浏览器中我不会期望看到该警报。

关于Javascript 历史记录无法检索 onpopstate 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9946091/

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