├── simple-history.js ├── index.html └── README.md /simple-history.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Simple History v0.5.0 3 | * 4 | * Copyright 2011, Jörn Zaefferer 5 | * Dual licensed under the MIT or GPL Version 2 licenses. 6 | */ 7 | (function(window, undefined) { 8 | 9 | var initial = location.href; 10 | 11 | window.SimpleHistory = { 12 | supported: !!(window.history && window.history.pushState), 13 | pushState: function(fragment, state) { 14 | state = state || {}; 15 | history.pushState(state, null, fragment); 16 | this.notify(state); 17 | }, 18 | replaceState: function(fragment, state) { 19 | state = state || {}; 20 | history.replaceState(state, null, fragment); 21 | }, 22 | notify: function(state) { 23 | this.matcher(location.pathname + location.search, state); 24 | }, 25 | start: function(matcher) { 26 | this.matcher = matcher; 27 | window.addEventListener("popstate", function(event) { 28 | // workaround to always ignore first popstate event (Chrome) 29 | // a timeout isn't reliable enough 30 | if (initial && initial === location.href) { 31 | initial = null; 32 | return; 33 | } 34 | SimpleHistory.notify(event.state || {}); 35 | }, false); 36 | } 37 | }; 38 | 39 | }(window)); -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Simple History 4 | Push One 5 | Push Two 6 | Push Three 7 | Replace One 8 | Replace Two 9 | Replace Three 10 | External Link to GitHub Repo 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple History 2 | 3 | This is a JavaScript library to lightly wrap the HTML5 history API, promoting the API 4 | in its most useful form, without a hashchange fallback or trying to fix irrelevant bugs. 5 | 6 | If you need an implementation with hashchange fallback, give 7 | [History.js](https://github.com/balupton/History.js/) a try. 8 | 9 | ## Usage 10 | 11 | Before you intialize any relevant event handlers, check if HTML5 history is supported: 12 | 13 | if (SimpleHistory.supported) { 14 | // event handlers 15 | } 16 | 17 | When support is available, use `SimpleHistory.start` to be notified whenever state changes: 18 | 19 | SimpleHistory.start(function(fragment) { 20 | // pass to router 21 | }); 22 | 23 | That callback is called whenever a state change occurs (push or pop, not on replace). Its not called 24 | on page load, assuming the inital rendering is done on the server. If not, you have to implement it 25 | elsewhere. 26 | 27 | To trigger a state change manually, use the `pushState` and `replaceState` methods: 28 | 29 | SimpleHistory.pushState(fragment); 30 | SimpleHistory.replaceState(fragment); 31 | 32 | As with the underlying API, `pushState` adds a history entry, `replaceState` replaces the 33 | current history entry. Use `replaceState` whenever you implement a redirect. 34 | 35 | Both also accept an second optional state argument (the first argument in the underlying implementation). 36 | 37 | SimpleHistory.pushState(fragment, state); 38 | SimpleHistory.replaceState(fragment, state); 39 | 40 | The title argument is not supported, as browsers don't implement that. Its trivial to implement where needed, 41 | just update document.title manually. 42 | 43 | ## Roadmap 44 | 45 | This library is intended to be kept as simple as it is now. It won't ever support a 46 | hashchange fallback, nor updating `document.title`. If you find an issue, please 47 | file a ticket. Of course, patches (via Pull Requests) are welcome. 48 | 49 | ## License 50 | 51 | Copyright 2012, Jörn Zaefferer 52 | 53 | Dual licensed under the MIT or GPL Version 2 licenses. 54 | --------------------------------------------------------------------------------