Created
November 18, 2011 16:07
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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