History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you
npm install historyjsWelcome to History.js
v1.8b2, June 22 2013
==================

HISTORY.md file for a detailed list of features, changes, solved issues and bugs- Follow the HTML5 History API as much as possible
- Provide a cross-compatible experience for all HTML5 Browsers (they all implement the HTML5 History API a little bit differently causing different behaviours and sometimes bugs - History.js fixes this ensuring the experience is as expected / the same / great throughout the HTML5 browsers)
- Provide a backwards-compatible experience for all HTML4 Browsers using a hash-fallback (including continued support for the HTML5 History API's data, title, pushState and replaceState) with the option to remove HTML4 support if it is not right for your application
- Provide a forwards-compatible experience for HTML4 States to HTML5 States (so if a hash-fallbacked url is accessed by a HTML5 browser it is naturally transformed into its non-hashed url equivalent)
- Provide support for as many javascript frameworks as possible via adapters; especially Dojo, ExtJS, jQuery, MooTools, Right.js and Zepto.
`` javascript
(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);
`
1. www.mysite.com
1. www.mysite.com/?state=1
1. www.mysite.com/?state=2
1. www.mysite.com/?state=3
1. www.mysite.com/?state=4
1. www.mysite.com/?state=3
1. www.mysite.com/?state=1
1. www.mysite.com
1. www.mysite.com/?state=3
> Note: These urls also work in HTML4 browsers and Search Engines. So no need for the hashbang (#!) fragment-identifier that google "recommends".
1. www.mysite.com
1. www.mysite.com/#?state=1&_suid=1
1. www.mysite.com/#?state=2&_suid=2
1. www.mysite.com/#?state=3&_suid=3
1. www.mysite.com/#?state=4
1. www.mysite.com/#?state=3&_suid=3
1. www.mysite.com/#?state=1&_suid=1
1. www.mysite.com
1. www.mysite.com/#?state=3&_suid=3
> Note 1: These urls also work in HTML5 browsers - we use replaceState to transform these HTML4 states into their HTML5 equivalents so the user won't even notice :-)
>
> Note 2: These urls will be automatically url-encoded in IE6 to prevent certain browser-specific bugs.
>
> Note 3: Support for HTML4 browsers (this hash fallback) is optional - why supporting HTML4 browsers could be either good or bad based on my app's use cases
- SUIDs (State Unique Identifiers) are used when we utilise a title and/or data in our state. Adding a SUID allows us to associate particular states with data and titles while keeping the urls as simple as possible (don't worry it's all tested, working and a lot smarter than I'm making it out to be).title
- If you aren't utilising or data then we don't even include a SUID (as there is no need for it) - as seen by State 4 above :-)http://www.mysite.com/#http://www.mysite.com/projects/History.js
- We also shrink the urls to make sure that the smallest url will be used. For instance we will adjust to become http://www.mysite.com/#/projects/History.js automatically. (again tested, working, and smarter).
- It works with domains, subdomains, subdirectories, whatever - doesn't matter where you put it. It's smart.
- Safari 5 will also have a SUID appended to the URL, it is entirely transparent but just a visible side-effect. It is required to fix a bug with Safari 5.
- Sure is, give it a download and navigate to the demo directory in your browser :-)
- If you are after something a bit more adventurous than a end-user demo, open up the tests directory in your browser and editor - it'll rock your world and show all the vast use cases that History.js supports.
- Download History.js and upload it to your webserver. Download links: tar.gz or zip
- Include History.js
- For Dojo v1.8+
` html`
- For ExtJs v1.8+
` html`
- For jQuery v1.3+
` html`
- For Mootools v1.3+
` html`
- For Right.js v2.2+
` html`
- For Zepto v0.5+
` html`
- For everything else
` html`
> Note: If you want to only support HTML5 Browsers and not HTML4 Browsers (so no hash fallback support) then just change the /html4+html5/ part in the urls to just /html5/. See Why supporting HTML4 browsers could be either good or bad based on my app's use cases
- For Commit RSS/Atom Updates:
- You can subscribe via the GitHub Commit Atom Feed
- For GitHub News Feed Updates:
- You can click the "watch" button up the top right of History.js's GitHub Project Page
- History.js is maintained by people like you. If you find a bug, report it to the GitHub Issue Tracker. If you've fixed a bug submit a Pull Request and add your fork to the Network Wiki Page.
- If you would like paid support and trainings, or have job offers, then refer to the Network Wiki Page. If you are qualified with History.js, then be sure to add your details to that page too.
- If your company uses History.js on your projects, and would like to see it grow and prosper (better documentation, bugfixes, upgrades, maintenance, etc.) and would love to become a corporate sponsor then do email sponsor@bevry.me
- If you would like free support for History.js, then post your question on Stackoverflow and be sure to use the history.js tag when asking your question.
- If you've created a website that uses History.js, or know of one, be sure to add it to the Showcase Wiki Page.
- If you'd love to +1 or like this project, then be sure to tweet about it and click the "watch" button up the top of its Project Page.
- For anything else, refer to the History.js GitHub Wiki Site.
Thanks! every bit of help really does make a difference!
- Firefox 4+
- Chrome 8+
- Opera 11.5+
- Safari 5.0+
- Safari iOS 4.3+
- IE 6, 7, 8, 9, (10)
- Firefox 3
- Opera 10, 11.0
- Safari 4
- Safari iOS 4.2, 4.1, 4.0, 3.2
#### States
- History.pushState(data,title,url) data
Pushes a new state to the browser; can be null or an object, title can be null or a string, url must be a stringHistory.replaceState(data,title,url)
- data
Replaces the existing state with a new state to the browser; can be null or an object, title can be null or a string, url must be a stringHistory.getState()
- data
Gets the current state of the browser, returns an object with , title and urlHistory.getStateByIndex
- History.getCurrentIndex
Gets a state by the index
- History.getHash()
Gets the current index
-
Gets the current hash of the browser
#### Adapter
- History.Adapter.bind(element,event,callback) History.Adapter.trigger(element,event)
A framework independent event binder, you may either use this or your framework's native event binder.
- History.Adapter.onDomLoad(callback)
A framework independent event trigger, you may either use this or your framework's native event trigger.
-
A framework independent onDomLoad binder, you may either use this or your framework's native onDomLoad binder.
#### Navigation
- History.back() History.forward()
Go back once through the history (same as hitting the browser's back button)
- History.go(X)
Go forward once through the history (same as hitting the browser's forward button)
-
If X is negative go back through history X times, if X is positive go forwards through history X times
#### Debug
- History.log(...) History.debug(...)
Logs messages to the console, the log element, and fallbacks to alert if neither of those two exist
- History.log
Same as but only runs if History.options.debug === true
- History.options.hashChangeInterval History.options.safariPollInterval
How long should the interval be before hashchange checks
- History.options.doubleCheckInterval
How long should the interval be before safari poll checks
- History.options.disableSuid
How long should the interval be before we perform a double check
- History.options.storeInterval
Force History not to append suid
- History.options.busyDelay
How long should we wait between store calls
- History.options.debug
How long should we wait between busy events
- History.options.initialTitle
If true will enable debug messages to be logged
- History.options.html4Mode
What is the title of the initial state
- History.options.delayInit
If true, will force HTMl4 mode (hashtags)
-
Want to override default options and call init manually.
- window.onstatechange window.onanchorchange
Fired when the state of the page changes (does not include hash changes)
-
Fired when the anchor of the page changes (does not include state hashes)
- History.js solves the following browser bugs:
- HTML5 Browsers
- Chrome 8 sometimes does not contain the correct state data when traversing back to the initial state
- Safari 5, Safari iOS 4 and Firefox 3 and 4 do not fire the onhashchange event when the page is loaded with a hashonpopstate
- Safari 5 and Safari iOS 4 do not fire the event when the hash has changed unlike the other browsersreplaceState
- Safari 5 and Safari iOS 4 fail to return to the correct state once a hash is replaced by a call / bug reportonpopstate
- Safari 5 and Safari iOS 4 sometimes fail to apply the state change under busy conditions / bug report
- Google Chrome 8,9,10 and Firefox 4 prior to the RC will always fire once the page has loaded / change recommendationtitle
- Safari iOS 4.0, 4.1, 4.2 have a working HTML5 History API - although the actual back buttons of the browsers do not work, therefore we treat them as HTML4 browsers
- None of the HTML5 browsers actually utilise the argument to the pushState and replaceState callsonhashchange
- HTML4 Browsers
- Old browsers like MSIE 6,7 and Firefox 2 do not have a eventurlencoded
- MSIE 6 and 7 sometimes do not apply a hash even it was told to (requiring a second call to the apply function)
- Non-Opera HTML4 browsers sometimes do not apply the hash when the hash is not document.title
- All Browsers
- State data and titles do not persist once the site is left and then returned (includes page refreshes)
- State titles are never applied to the History.back()
- ReplaceState functionality is emulated in HTML4 browsers by discarding the replaced state, so when the discarded state is accessed it is skipped using the appropriate / History.forward() call?key=a%20b%252c
- Data persistance and synchronisation works like so: Every second or so, the SUIDs and URLs of the states will synchronise between the store and the local session. When a new session opens a familiar state (via the SUID or the URL) and it is not found locally then it will attempt to load the last known stored state with that information.
- URLs will be unescaped to the maximum, so for instance the URL will become ?key=a b c. This is to ensure consistency between browser url encodings.onpopstate
- Changing the hash of the page causes to fire (this is expected/standard functionality). To ensure correct compatibility between HTML5 and HTML4 browsers the following events have been created:window.onstatechange
- : this is the same as the onpopstate event except it does not fire for traditional anchorswindow.onanchorchange
- : this is the same as the onhashchange` event except it does not fire for states
Licensed under the New BSD License
Copyright © 2014+ Bevry Pty Ltd
Copyright © 2011-2013 Benjamin Arthur Lupton
For support see the Getting Support section.