History.js優(yōu)雅的歷史api
History.js 很優(yōu)雅的支持 HTML5 History/State APIs (pushState, replaceState, onPopState),并使之在所有瀏覽器上都可用。包括繼續(xù)支持 data, titles, replaceState. Supports jQuery, MooTools and Prototype. 對(duì)于 HTML5 瀏覽器而言意味著你可以直接修改 URL 地址。
在線演示:http://browserstate.github.io/history.js/demo/
示例代碼:
(function(window,undefined){
// 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
});
// 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);評(píng)論
圖片
表情
