Javascript Can';我搞不懂历史

Javascript Can';我搞不懂历史,javascript,jquery,history.js,html5-history,Javascript,Jquery,History.js,Html5 History,我正试图实现对ajax控制的站点的导航,但遇到了一些奇怪的错误 我使用的是History JS,只有HTML5版本 这是我初始化它的方式: function initializeHistory() { var History = window.History; if ( !History.enabled ) { console.log("Not enabled!"); return false; } // Changing th

我正试图实现对ajax控制的站点的导航,但遇到了一些奇怪的错误

我使用的是History JS,只有HTML5版本

这是我初始化它的方式:

function initializeHistory() {

    var History = window.History;
    if ( !History.enabled ) {
        console.log("Not enabled!");
        return false;
    }

    // Changing the main page state to -1.
    History.replaceState({id:-1}, baseTitle, baseUrl);

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){
        var State = History.getState();
        console.log(History.savedStates);
        if (historyData.manualStateChange)
        {
            if (State.data.id=='-1') alert('History start');
            historyData.allowHistoryPushPop=false;
            var gotoState=historyData.data[State.data.id];
            var currentState=historyData.currentNavigation;

            /* Some code here to revert to the previous state */

            historyData.allowHistoryPushPop=true;
      }

      History.log(State.data, State.title, State.url);
   });
};
我正在使用一个名为historyData的全局对象,其中存储以下内容:

var historyData={
    data:                   new Array(), //an array which contains objects that refer to the data that needs to be shown, when back or forward button is pushed
    manualStateChange:      true, //using this to figure out if the back or forward button was pressed in the browser, or if I am adding or removing a state from History programmatically
    allowHistoryPushPop:    true, //using this to prevent history from being changed in certain situations
    currentNavigation:      {
        page:   'dashboard',
        pageid: null,
        module: null,
        moduleid: null
    }, // stores the current object from the historyData.data array
    status: true // boolean that enables or disables History on my page
}
每当我单击页面中的链接时,就会触发一个名为navigation的函数,该函数会更改历史记录的状态,并最终运行一系列函数来显示用户请求的页面。导航功能的相关部分如下所示:

function navigation(gotofunc, navData) {
    if ((historyData.allowHistoryPushPop) && (historyData.status))
    {
        if (navData['urlData']==null) // if this is null, then the title and the url for the asked page, will be returned by the server, after an ajax call, so we set it to the current url and current title
        {
            var curState=History.getState();
            urlData={
                title:  curState.title,
                url:    curState.url
            };
        } else {
            urlData=navData['urlData'];
            if (!urlData['title']) urlData['title']=curState.title;
            if (!urlData['url']) urlData['url']=curState.url;
        }
        navData['parameters']=new Array();
        if (arguments.length>2) for (i=2;i<arguments.length ;i++) navData['parameters'].push(arguments[i]);
        historyData.manualStateChange=false; // we are making a programmatic change, so we don't want the binded 'statechange' to fire
        historyData.data.push(navData); // store the history data in our own array
        History.pushState({id:historyData.data.length-1}, urlData['title'], urlData['url']); // push the History state, and set it's id to point to our newly added array element
        historyData.manualStateChange=true; // re-enable the manual state change
    }
}
这在很大程度上是可行的。如果我向前浏览几页,然后向后浏览,一切都会很好地工作,如果我再次向前浏览(都是通过使用浏览器的后退和前进按钮),一切都会很好地工作。只有一个例外:如果我加载页面,加载子页面,然后尝试单击“上一步”按钮,这一行永远不会触发:

   if (State.data.id=='-1') alert('History start');
当我只向前浏览了一页时,我基本上不会发现我回到了头版

另一件奇怪的事情是(可能这也是导致我最初问题的原因):我尝试获取历史对象的savedStates,以查看发生了什么,奇怪的是,当我使用replaceState事件时,它在savedStates中添加了一个新状态。添加的一个对象具有正确的数据id,另一个对象具有以前的数据id

问题可能是由什么引起的?我的脚本中是否有错误?还是这完全正常?(在replaceState之后向History.savedStates添加多个对象的部分)


提前谢谢你的帮助

如果我用第一个replaceState替换url或页面标题,程序会在我返回首页时意识到这一点,我不知道为什么,但在此之前,这是我能想到的最佳解决方案

   if (State.data.id=='-1') alert('History start');