Skip to content

Instantly share code, notes, and snippets.

@findzen
Created February 3, 2012 17:46
Show Gist options
  • Select an option

  • Save findzen/1731354 to your computer and use it in GitHub Desktop.

Select an option

Save findzen/1731354 to your computer and use it in GitHub Desktop.
History.js HTML4 / HTML5 parity
// save hash value for HTML5 browsers arriving to site via HTML4 hashed url
// this should be done before $(document).ready and History.js is loaded
window.initHash = window.location.hash;
$(document).ready(function() {
var baseURL = History.getBaseUrl(),
currentURL = History.getState().url,
relativeURL = currentURL.replace( baseURL, '' );
if ( !Modernizr.history && currentURL !== baseURL ) {
// if browser doesn't support HTML5 history and this isn't the root url,
// redirect to the root url + hash
document.location.href = baseURL + '#./' + relativeURL;
return;
} else if ( Modernizr.history && window.initHash.length ) {
// if this browser supports HTMLS history and the initial url included a hash,
// load the content via ajax (assumes load method is defined elsewhere)
load( History.getState().url );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment