Skip to content

Instantly share code, notes, and snippets.

@nexus-uw
Created May 29, 2017 19:30
Show Gist options
  • Save nexus-uw/96bce6689753e15add08f9d2e508094e to your computer and use it in GitHub Desktop.
Save nexus-uw/96bce6689753e15add08f9d2e508094e to your computer and use it in GitHub Desktop.
save/retrieve state in the query string
function saveParamsToUrlQuery(state) {
const queryString = Object.keys(state)
.reduce((res, key) => state[key] ?
res += `${encodeURIComponent(key)}=${encodeURIComponent(state[key])}&` :
res,
'');
history.pushState({}, 'generate-routes', window.location.pathname + '?' + queryString);
}
function loadInputFromUrl() {
const queryStrings = (window.location.search.split('?')[1] || '').split('&');
return queryStrings.reduce((result, qs) => {
const f = qs.split('=');
result[decodeURIComponent(f[0])] = decodeURIComponent(f[1]);
return result;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment