在IE9中使用history.js [英] using history.js with IE9

查看:173
本文介绍了在IE9中使用history.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

从history.js插件提供的文档中,其用途尚不清楚.我已经在模板的<head></head>部分中添加了插件,但是在IE9上,我仍然收到错误消息:

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

错误输出的功能如下

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

推荐答案

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

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)

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

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

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

这篇关于在IE9中使用history.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆