我以以下方式将 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 中测试时,会出现以下类似警报的消息:
弹出窗口中的 url 是我所期待的,即使在浏览器中它显示为
http://www.foobar.com/.../#?Fixed
也就是说,有一个额外的哈希。不过,让我担心的是错误信息 - 任何线索?