Skip to content

Instantly share code, notes, and snippets.

@timmywil
Created November 18, 2011 16:07
Show Gist options
  • Save timmywil/1376879 to your computer and use it in GitHub Desktop.
Save timmywil/1376879 to your computer and use it in GitHub Desktop.
A simple pushState plugin that falls back to hashChange (only for going back)
/**
* @license backPushState.js
* A simple pushState library that falls back to hashChange (only for going back)
* Copyright (c) 2011 timmy willison
* Dual licensed under the MIT and GPL licenses.
* http://timmywillison.com/licence/
*/
define([ 'jquery' ],
function( $ ) {
"use strict";
return (function( window, document, history, location ) {
if ( history && history.pushState ) {
return function( title, url ) {
var curPath = document.location.pathname;
history.pushState( null, title, url );
document.title = title;
$(window).bind('popstate', function() {
document.location.href = curPath;
});
};
}
location = document.location;
return function( title, url ) {
var curPath = location.pathname;
url = '#!/' + url.replace( curPath, '' ).replace(/^\//, '');
location.hash = url;
document.title = title;
setTimeout(function() {
$(window).one('hashchange', function() {
location.href = curPath;
});
}, 0);
};
})( window, window.document, window.history );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment