using history.js with IE9

2019-06-06 22:33发布

In one of my applications i use window.history.pushState() method to update the window URL. But as IE < 10 does not support HTML5's history api, I was looking for an alternative. Answers to a lot of other questions on SO suggested to use history.js plugin.

From the documentation provided by the history.js plugin it's usage is not really clear. I have added the plugin to the <head></head> section in my templates but on IE9 i am still receiving the error that says:

SCRIPT438: Object doesn't support property or method 'pushState'
params.js, line 37 character 5

the function that errors out is as follows

/**
 * updates the window url according to the given parameters.
 */
function updateUrl(params) {
    var path = window.location.pathname;
    var url = $.param.querystring(path, params);
    url = decodeURIComponent(new_url).replace("#", "", "g");
    window.history.pushState(null, null, new_url);
}

1条回答
劳资没心,怎么记你
2楼-- · 2019-06-06 22:50

You need to initiatialize the History and State variables for it to work.

(function(window, undefined) {
    // Define variables
    var History = window.History,
        State = History.getState();

    // Bind statechange event
    History.Adapter.bind(window, 'statechange', function() {
        var State = History.getState();
    });
})(window)

and now you can use pushState method for all browsers as follows:

History.pushState(null, null, '?your=hash')
查看更多
登录 后发表回答