Skip to content

Instantly share code, notes, and snippets.

@ericmuyser ericmuyser/pushState
Created Feb 13, 2015

Embed
What would you like to do?
React Router Pushing State
var dummyLink = document.createElement('a');
var absolutify = function (url) {
dummyLink.href = url;
return dummyLink;
};
$('body').on('click', 'a', function(e) {
var $anchor = $(this);
if (!$anchor.attr('href')) {
return;
}
// Don't process hash changes
if ($anchor.attr('href')[0] === '#') {
return;
}
var parsed = absolutify($anchor.attr('href'));
var href = parsed.href;
var siteUrl = window.location.origin;
if (!href) {
return;
}
if (href.substr(0, siteUrl.length) === siteUrl) {
var path = parsed.pathname;
if (!path) {
return;
}
// Hmmmmm, what to do here?
// History.pushState({}, '', path);
// or router.transitionTo(path.slice(1));
// or ...
router.teardown();
router = ReactRouter.run(Router.routes, path, InitializeRouter);
e.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.