2012-03-30 43 views
0

值这里是我的代码:的Javascript历史不能检索onpopstate

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,因为我期望它
  • 按回来创建警报,但值是undefined而不是8正如我所期望的
+0

“.pushState()”的“title”参数被Firefox忽略,也可能是其他浏览器。 – Pointy 2012-03-30 15:39:36

+0

'event.state.selectedMenu'对我来说在Chrome中工作正常 – Esailija 2012-03-30 15:39:56

+0

@Pointy我使用的是Chrome,希望它像Chrome网上应用店一样工作https://chrome.google.com/webstore/category/app/8 -教育 – 2012-03-30 15:43:44

回答

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

  2. 当您调用pushState(),然后使用后退按钮时,您不会回到最后一个pushState(实际上是当前状态),而是返回到之前的状态。在你的情况,这听起来像你看到页面加载状态,这就是为什么selectedMenu是未定义的。 顺便说一下,只有Chrome浏览器会激活poppop以匹配初始页面加载,所以在其他浏览器中,我不希望看到该提醒。