Statechange is firing whenever i do a push-state

后端 未结 3 1404
野的像风
野的像风 2020-12-17 14:54

I am using history.js to handle back button. In history.js statechange is firing whenever i do a pushstate. Why?

3条回答
  •  时光说笑
    2020-12-17 15:18

    After trying to accomplish this for a day now, I finally found the solution here: https://github.com/browserstate/history.js/issues/47#issuecomment-25750285

    The code is pretty damn simple, the following is quoted from the link:

    When you push your state

    History.pushState({
        _index: History.getCurrentIndex(),
        someData: ...
    }, someTitle, someUrl);
    

    and then in the event binding

    History.Adapter.bind(window, 'statechange', function () {
        var currentIndex = History.getCurrentIndex();
        var internal = (History.getState().data._index == (currentIndex - 1));
        if (!internal) {
            // your action
        }
    });
    

提交回复
热议问题