0

这是我的代码:

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,但不会the title to use像我预期的那样更新标题
  • 按下它会创建警报,但值undefined8我预期的不同
4

1 回答 1

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

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

于 2012-04-04T03:44:04.587 回答