【发布时间】:2013-03-22 15:16:57
【问题描述】:
我已经阅读了 stackoverflow 上关于 history.js 的所有帖子,包括 this、this 和 this,并查看了 source code,但作为 javascript/jquery 的新手,我无法弄清楚了解如何实际实现对 html 5 历史记录的支持和回退以支持 ie8/9 等 html4 浏览器。我可以从尽可能多地呈现一致的 URL 中体会到 UX 的好处,这如何解决深度链接并允许我想要实现的书签,但是当我尝试在我的网站上实际使用它时我有点迷失了。
将 history.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);
});
// Change our States
History.pushState({state:1}, "State 1", "?state=1"); // logs {state:1}, "State 1", "?state=1"
History.pushState({state:2}, "State 2", "?state=2"); // logs {state:2}, "State 2", "?state=2"
History.replaceState({state:3}, "State 3", "?state=3"); // logs {state:3}, "State 3", "?state=3"
History.pushState(null, null, "?state=4"); // logs {}, '', "?state=4"
History.back(); // logs {state:3}, "State 3", "?state=3"
History.back(); // logs {state:1}, "State 1", "?state=1"
History.back(); // logs {}, "Home Page", "?"
History.go(2); // logs {state:3}, "State 3", "?state=3"
})(window);
//Change our states 是否是所有新代码所在的位置,因为此代码仅提供了历史控件的示例?
或者我应该编写自己的代码来代替整个代码块(鉴于我对编码的新手,我现在使用 jquery 来帮助我)。
我正在阅读此 article 关于动态内容加载并尝试在我的网站上实现的内容(我可以让这段代码工作,但我知道它在 ie8/9 中无法正常运行),但我在尝试时遇到了麻烦弄清楚如何与history.js结合
另外,其次,我想弄清楚 history.js 如何与modernizr 配合使用?
它是不是modernizr.history 的替代品(它在哪里进行测试,如果不支持.js 则退回到典型的页面加载)还是会像这样运行:
if (Modernizr.history) {
//Code goes here that works if it's HTML 5 Browser with .history support? I know some HTML5 browsers deal with .history oddly (read buggy) so what happens in those cases?
} else {
//code from above goes here? with function(window, undefined){...etc... ?
}
【问题讨论】:
标签: jquery ajax browser-history history.js