Restoring content when clicking back button with History.js

与世无争的帅哥 提交于 2019-12-02 19:29:33

Ok I got it, also thanks to Tobias Cohen for the hint.

One has to store the loaded data in the history object (State.data). First let's see how the statechange callback changed:

History.Adapter.bind(window, 'statechange', function()
{

    var State = History.getState();

    $.each(State.data.clips, function(key, val)
    {
        $(key).replaceWith(val);
    });

    History.log(State.data, State.title, State.url);

});

As you can see, on each statechange I can access State.data.clips and replace the html content.

NOTE: A statechange does also happen when calling History.pushState(). That means in my initial question the second code snippet is wrong in the fact that I do the content manipulation in there. There's no need for it. Just call History.pushState() and do any content manipulation within the statechange callback.

So for completeness, this is how I push the clips into the history object:

History.pushState({state:1, clips:data.clips}, data.title || document.title, 'http://127.0.0.1/site/www/');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!