using history.js with IE9

隐身守侯 提交于 2019-12-08 07:54:18

问题


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:


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')


来源:https://stackoverflow.com/questions/19634513/using-history-js-with-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!