1

我以以下方式将 History.js 用于 jQuery(v1.7.1,html4+html5 捆绑包)

在我的 js 文件顶部准备:

(function(window,undefined){

    // Prepare
    var History = window.History; // Note: We are using a capital H instead of a lower h
    if ( !History.enabled ) {
         // History.js is disabled for this browser.
         // This is because we can optionally choose to support HTML4 browsers or not.
        return false;
    }

    // Bind to StateChange Event
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        History.log(State.data, State.title, State.url);
    });

})(window);

然后在我的代码中进一步向下:

    $foo.click(function(e) {
        e.preventDefault();
        History.pushState({}, "", "?" + bar);
    });

基本上我想要实现的是,每次单击某些项目时,都会将查询字符串"?" + bar附加到 url 并且 pushState 将其存储在浏览器历史记录中。这在 FF 最新版本中运行良好,但在 IE7 中测试时,会出现以下类似警报的消息:

IE7 中的 History.js 错误 弹出窗口中的 url 是我所期待的,即使在浏览器中它显示为

http://www.foobar.com/.../#?Fixed

也就是说,有一个额外的哈希。不过,让我担心的是错误信息 - 任何线索?

4

2 回答 2

0

仅作记录,如果有人再次出现在此页面上 - 我已经隔离了脚本并且错误不再发生。经过漫长而乏味的测试,我发现这是与专有 jquery 插件冲突引起的,并修复了它。

于 2012-10-20T17:07:47.237 回答
0

原因:您需要从代码中删除“History.log()”...在 IE 中,此 History.log 正在将日志输出写入警报框...

于 2013-01-25T14:11:46.987 回答