Skip to content

Instantly share code, notes, and snippets.

@moesoha
Last active December 24, 2020 15:57
Show Gist options
  • Save moesoha/b36e5846226e4c15a5bed76a01e2ced9 to your computer and use it in GitHub Desktop.
Save moesoha/b36e5846226e4c15a5bed76a01e2ced9 to your computer and use it in GitHub Desktop.
Get SearchParams
// compatibility: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
const getQueryParams = s => Object.fromEntries(s.split('&').map(s => s.split('=')).map(a => [a.shift(), a.join('=')].map(s => decodeURIComponent(s || ''))).filter(([k, _]) => k.length > 0));
// window.$_GET = getQueryParams(window.location.search.slice(1));
function getQueryParams(search) {
var params = {};
search
.split('&')
.map(function (s) { return s.split('='); })
.map(function (a) { return [a.shift(), a.join('=')].map(function (s) { return decodeURIComponent(s || ''); }); })
.filter(function (a) { return a[0].length > 0; })
.forEach(function (a) { params[a[0]] = a[1]; })
;
return params;
}
// window.$_GET = getQueryParams(window.location.search.slice(1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment