Search code examples
javascriptjqueryajaxhistory.js

Facing an issue with back button click event using history.js


I have implemented State Aware URL on ajax call using history.js(using pushState method). Now when I click back button of any browser, it should load content of previous state using my ajax method.For this I have done following:

$(function () {
    // history.js 
    History.Adapter.bind(window, 'statechange', function () {
        var State = History.getState();
        callAjax(State.data.loadUrl);
    });
});

But this code executes on every state change as well as on forward/backward. I just want to execute it on the forward / backward event of any browser. What shall I do to get this?

Any help is appreciated!


Solution

  • I have done following:

    $(function () {
    // history.js
    History.Adapter.bind(window, 'statechange', function () {
        if (manualStateChange == true) {
            var State = History.getState();
            callAjax(State.data.loadUrl);
        }
        manualStateChange = true;
    });});
    

    And when I change the satate of URL I set the value :

    manualStateChange = false;

    If I set true value for manualStateChange in jquery function then on every state change it calls the function. That's why I have removed that line from my code and it works for me.