【问题标题】:using history.js with IE9在 IE9 中使用 history.js
【发布时间】:2013-10-28 12:12:49
【问题描述】:

在我的一个应用程序中,我使用window.history.pushState() 方法来更新窗口 URL。但由于IE < 10 不支持HTML5 的历史API,我一直在寻找替代方案。 SO上很多其他问题的答案建议使用history.js插件。

从 history.js 插件提供的文档来看,它的用法并不是很清楚。我已将插件添加到模板中的 <head></head> 部分,但在 IE9 上我仍然收到错误消息:

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

报错的函数如下

/**
 * 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);
}

【问题讨论】:

    标签: html internet-explorer-9 pushstate history.js


    【解决方案1】:

    您需要初始化 History 和 State 变量才能使其工作。

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

    现在您可以对所有浏览器使用pushState 方法,如下所示:

    History.pushState(null, null, '?your=hash')
    

    【讨论】:

    • 您在 history.js 中使用了 'window.history' 而不是 'window.History'。与初始化无关=)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2012-07-06
    • 1970-01-01
    • 2013-07-15
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多