Skip to content

Instantly share code, notes, and snippets.

@iOnline247
Last active March 16, 2021 04:52
Show Gist options
  • Select an option

  • Save iOnline247/0e3a4d781b4bb42d0058ded936691669 to your computer and use it in GitHub Desktop.

Select an option

Save iOnline247/0e3a4d781b4bb42d0058ded936691669 to your computer and use it in GitHub Desktop.
Get QueryStrings from URL (default) or string
function getQueryStrings(v) {
const result = {};
const rstripLeadingQuery = /^(:?\?|#)/;
const queryString = (v ? String(v) : window.location.search).replace(
rstripLeadingQuery,
''
);
const re = /([^&=]+)=([^&]*)/g;
let m;
// eslint-disable-next-line no-cond-assign
while ((m = re.exec(queryString))) {
result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return result;
}
// Usage:
// const queryStrings = getQueryStrings();
// const queryStrings = getQueryStrings('?Id=23829&lcid=1033&token=easterEgg');
// const queryStrings = getQueryStrings('Id=23829&lcid=1033&token=easterEgg'); // Missing the leading `?`
// const queryStrings = getQueryStrings('#here=nope'); // parses hashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment