Skip to content

Instantly share code, notes, and snippets.

@bspavel
Forked from sindresorhus/query-string-example.js
Created July 12, 2018 08:11
Show Gist options
  • Save bspavel/997e7177bdce4a890c0840db9ec02241 to your computer and use it in GitHub Desktop.
Save bspavel/997e7177bdce4a890c0840db9ec02241 to your computer and use it in GitHub Desktop.
Get, set, modify url parameters in a query string with JavaScript.
// Get the queryString module from:
// https://github.com/sindresorhus/query-string
console.log(location.href);
// http://sindresorhus.com/?foo=bar
console.log(location.search);
// ?foo=bar
var parsed = queryString.parse(location.search);
console.log(parsed);
// {foo: 'bar'}
parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
location.search = queryString.stringify(parsed);
console.log(location.search);
// ?foo=unicorn&ilike=pizza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment