我已经阅读了 stackoverflow 上关于 history.js 的所有帖子,包括this、this和this并查看了源代码,但作为 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 来帮助我)。
我正在阅读这篇关于动态内容加载并尝试在我的网站上实现的文章(我可以让这段代码正常工作,但我知道它在 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... ?
}